Question

I have a mediaplayer that plays music in the activity below. The thing is, the music transfers to all other activities and thats fine. But when I start the music, go to another activity and then come back to the activity below, the togglebutton has changed from on to off. Therefore, if I click on the togglebutton, the music starts again even though the music is still playing so now the music is playing twice at once! How do I make the togglebutton know that the music is playing so when I come back to the activity, the togglebutton is "on" from the beginning? It's "off" by default.

public class SpelaActivity extends Activity {

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spela);

    final MediaPlayer mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.ljudfil);

    ToggleButton musikknapp = (ToggleButton) findViewById(R.id.togglemusik);
    musikknapp.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        if (isChecked) {
            mediaPlayer.start();
        } else {
            mediaPlayer.pause();
        }
    }
});
    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.release();
        }
    });

}
}
Was it helpful?

Solution 3

Managed to get the mediaplayer to stop upon back button click:

final ToggleButton togglemusik = (ToggleButton) findViewById(R.id.togglemusik);
    mp = MediaPlayer.create(this, R.raw.ljudfil);
    togglemusik.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (togglemusik.isChecked()) {
                mp.start();
            } else {
                mp.pause();
            }   
        }
    });
}
    @Override
    public void onPause() {
        super.onPause();
        if (mp !=null) {
            mp.release();
            mp = null;
        }
        }

OTHER TIPS

You need save the ToggleButton State. Try this code:

public class MainActivity extends Activity {

    private ToggleButton toggleButton;
    private static Bundle bundle = new Bundle();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
    }



    @Override
    public void onPause() {
        super.onPause();
        bundle.putBoolean("ToggleButtonState", toggleButton.isChecked());
    }

    @Override
    public void onResume() {
        super.onResume();
        toggleButton.setChecked(bundle.getBoolean("ToggleButtonState",false));
    }

add these condition if(!mediaPlayer.isPlaying())

 public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                if(!mediaPlayer.isPlaying())
                mediaPlayer.start();
            } else {
                if(!mediaPlayer.isPlaying())
                mediaPlayer.pause();
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top