简体   繁体   English

libgdx中的音乐流不起作用(JAVA)

[英]Music streaming in libgdx doesn't work (JAVA)

So I've been working on a libGDX project, and I have encountered a problem here: 因此,我一直在从事libGDX项目,并且在这里遇到了一个问题:

 public class Main extends ApplicationAdapter implements Screen {
      (...)
 @Override
 public void create() {
 (...)
 //Starts playing Main theme Music
 MusicPlayer.determineMusic(mainTheme, splash);

 }

 public void render() {

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();

        //was:
        //splash.splashRenderer(mainTheme, batch, splashSprite);

        //changed to:
        splash.splashRenderer(batch, splashSprite);

    batch.end();

    System.out.println("******************End of Render******************");
}


public class SplashScreen {


 Music gameTheme ;

    public SplashScreen(){
    }

    public void splashRenderer(SpriteBatch batch, Sprite splashSprite) {
    batch.draw(splashSprite, 0, 0, MyConstants.WINDOW_WIDTH,                    MyConstants.WINDOW_HEIGHT);
    }

   @Override
   public void show() {
       //gameTheme = Gdx.audio.newMusic(Gdx.files.internal("blah.mp3"));
       //gameTheme.play();
   }

   @Override
   public void hide() {
       //gameTheme.dispose();
   }

   @Override
   public void dispose() {
      //gameTheme.dispose();

   }

}

public class MusicPlayer {

   protected MusicPlayer(){
   }

   public static void determineMusic(Music Theme, AbstractScreen screen){

       if ((screen instanceof GameScreen) == false){
           Theme.play();
       }
   }

}

The code so far basically just loads a music file and a sprite and draws it on the screen through the splashRenderer method, but my problem is that the music doesn't start. 到目前为止,代码基本上只加载了一个音乐文件和一个精灵,并通过splashRenderer方法将其绘制在屏幕上,但是我的问题是音乐没有开始。 Any help will be greately appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

EDIT1~ I implemented Screen, changed the method to splashRenderer(SpriteBatch, Sprite) & made the music file load and play in the show method and dispose() it in the hide and in the dispose methods. EDIT1〜我实现了Screen,将方法更改为splashRenderer(SpriteBatch,Sprite)并在show方法中加载和播放音乐文件,并在hide和dispose方法中将其dispose()。 I didn't add anything to the pause() method because I think it is done automatically. 我没有添加任何东西到pause()方法,因为我认为它是自动完成的。

EDIT2~ I noticed that the music doesn't start this time either. EDIT2〜我注意到这一次音乐也没有开始。 The solution I came up with is to make a new class MusicPlayer() that will determine whether or not to play and which track. 我想出的解决方案是制作一个新类MusicPlayer(),该类将确定是否播放以及播放哪个曲目。 The musicPlayer is first called explicitly in the create() and will be recalled in when needed probably when handling mouseEvents. 首先在create()中显式调用musicPlayer,并且可能在处理mouseEvents时在需要时对其进行调用。

Please provide feedback on these changes (good/bad/to be avoided for this and that reason etc). 请提供有关这些更改的反馈(好的/坏的/为此要避免的原因等)。 Thanks! 谢谢!

I did the following: 我做了以下事情:

public abstract class BaseScreen implements Screen, InputProcessor
{
     public BaseScreen() 
     {
         windowWidth = Gdx.graphics.getWidth();
         windowHeight = Gdx.graphics.getHeight();
         AudioManager.getScreenThemes().put(this, getScreenMusic());
     }

     public void pause()
     {
         AudioManager.pause();
     }

     public void resume()
     {
         AudioManager.play();
     }

     public void show()
     {
         Gdx.input.setInputProcessor(this);
         AudioManager.switchMusic(this);
     }


     public abstract Music getScreenMusic();
     ...
}

Another Screen: 另一个屏幕:

@Override
public Music getScreenMusic()
{
    return Resources.soundGameTheme;
}

AudioManager: AudioManager:

static
{
    screenThemes = new HashMap<Screen, Music>();
    currentlyPlayed = null;
}

public static boolean getSoundEnabled()
{
    return MyGame.getPreferences().getBoolean(Resources.soundEnabledKey);
}

private static Map<Screen, Music> screenThemes;

public static Music currentlyPlayed;

public static void play()
{
    if(getSoundEnabled() != false)
    {
        if(currentlyPlayed != null)
        {
            if(currentlyPlayed.isPlaying() == false)
            {
                currentlyPlayed.play();
            }
        }
    }
}

public static void initialize()
{
    Resources.soundMainMenuTheme = Gdx.audio.newMusic(Gdx.files.internal("data/main_menu.mp3"));
    Resources.soundGameTheme = Gdx.audio.newMusic(Gdx.files.internal("data/game.mp3")); 
    Resources.soundMainMenuTheme.setLooping(true);
    Resources.soundGameTheme.setLooping(true);
}

public static void stop()
{
    if(currentlyPlayed != null)
    {
        if(currentlyPlayed.isPlaying() == true)
        {
            currentlyPlayed.stop();
        }
    }
}

public static void pause()
{
    if(getSoundEnabled() != false)
    {
        if(currentlyPlayed != null)
        {
            if(currentlyPlayed.isPlaying() == true)
            {
                currentlyPlayed.pause();
            }
        }
    }
}

public static void switchMusic(Screen screen)
{
    if(screenThemes.get(screen) != currentlyPlayed)
    {
        if(screenThemes.get(screen) != null)
        {
            stop();
            currentlyPlayed = screenThemes.get(screen);
            play();
        }
    }
}

public static void dispose()
{
    Resources.soundMainMenuTheme.dispose();
    Resources.soundGameTheme.dispose();
    screenThemes = null;
}

public static Map<Screen, Music> getScreenThemes()
{
    return screenThemes;
}

Not the prettiest thing, I admit - I should have used a singleton of some sort like an enum, rather than make it static in this fashion. 我承认,这不是最漂亮的东西-我应该使用某种枚举之类的单例,而不是以这种方式使其静态。 But it worked, although it basically plays a specific song when you navigate to a specific Screen and changes the song if the song to be played on the screen is different. 但这确实有效,尽管当您导航到特定的Screen时它基本上会播放特定的歌曲,并且如果要在屏幕上播放的歌曲不同,则可以更改歌曲。

EDIT: 编辑:

Preferences: 首选项:

public class MyGame extends Game
{
    private static Preferences preferences;

    @Override
    public void create()
    {
        ... 
        preferences = Gdx.app.getPreferences(Resources.preferencesName);
        ...
    }

    public static Preferences getPreferences()
    {
        return preferences;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM