3

I want background music to be played over various screens of my game the music is initially started in the first screen class:

    boolean backgroundMusicPlaying = backgroundMusic.isPlaying();

    public MainMenuScreen(Game1 gam){
    (...)
    if(backgroundMusicPlaying != true){
                backgroundMusic.play();
                backgroundMusic.setVolume(0.3f);
                backgroundMusic.setLooping(true);
                backgroundMusicPlaying = true;
                }
   (...)
 }

Problem

The problem is when i return to this class after I have been to a previous screen in the game it restarts the music but i don't want this i want it to be a continuous loop.

An example of how the screen's are changed to and from this class/screen:

game.setScreen(new playOptions(game));
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
useruseruser
  • 73
  • 1
  • 6

2 Answers2

0

Move the backgroundMusic.isPlaying() call inside the method?

public MainMenuScreen(Game1 gam){
(...)
boolean backgroundMusicPlaying = backgroundMusic.isPlaying();
if(backgroundMusicPlaying != true){
            backgroundMusic.play();
            backgroundMusic.setVolume(0.3f);
            backgroundMusic.setLooping(true);
            backgroundMusicPlaying = true;
            }

(...)

fishinear
  • 6,101
  • 3
  • 36
  • 84
0

Looks like you're calling some non-UI code in your activity. So you need to wrap your music player in an AsyncTask. This way your player won't block UI and won't be tied to it. This should like something like the following code.

public class MusicPlayer extends AsyncTask<Void, Void, Void>
{
    public String filename;
    public boolean backgroundMusicPlaying;
    public ??? backgroundMusic;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //this method will be running on UI thread
    }
    @Override
    protected Void doInBackground(Void... params) {
        //this method will be running on background thread so don't update UI frome here
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread
    }


    public void playMusic() {
        // stub
    }

    public void pauseMusic() {
        // stub
    }

    public void setVolume(float level) {
        // stub
    }

    // etc
}

Just implement some methods in order to control the MusicPlayer, or just wrap those of backgroundMusic. Or just let backgroundMusic's class extend the AsyncTask class.

Read http://developer.android.com/reference/android/os/AsyncTask.html for more information.

Max Malysh
  • 29,384
  • 19
  • 111
  • 115
  • sorry using libgdx and want a java logic based solution so that it can be implemented for cross platform use – useruseruser Nov 22 '14 at 23:29
  • So implement all your logic in your own class and just control it using this method. So playMusic() of async task would call backgroundMusic.play() and so on. Async tasks were developed to sync the UI threads and the Non-UI threads. – Max Malysh Nov 22 '14 at 23:32
  • Ok thanks that makes more sense just as a side not how would you recommend stopping this class/method with a boolean value you change etc? – useruseruser Nov 23 '14 at 00:04
  • Just add a boolean field "isPlaying". Create getter and setter for it. These methods should change the "isPlaying" field and run some methods of backgroundMusic if you have them. As I see, you have .play() method at least. If there is no .pause() method, add it to the backgroundMusic's class if possible. If not, then just create new instance of backgroundMusic every time you need to stop it. – Max Malysh Nov 23 '14 at 00:05