[SOLVED] MediaPlayer mp3 won’t resume after pause, known solutions not working


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

My activity is playing mp3 file while is active, and my intention is to pause it while app takes user to another activity and resume when this activity is again active. Solution that was here around seems to be the right one but unfortunately id doesn’t work, the audio file every time starts from beginning. My code is obvious:

private int length;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(this,  R.raw.bensound_thejazzpiano);
        mediaPlayer.setLooping(true);
        mediaPlayer.setVolume(0.4f,0.4f);
        mediaPlayer.start();
}

 @Override
    protected void onPause() {
        super.onPause();
        mediaPlayer.stop();
        length = mediaPlayer.getCurrentPosition();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mediaPlayer.seekTo(length);
 mediaPlayer.start();

    }

I would also be satisfied with methods that mutes sound and restores the volume after goin back to activity (and muted tune can go on in background), but replacing start/stop methods with setVolume also doesn’t provide to any results…

Maybe there are wrong methods I’ve overridden?

Solution

You have to “pause”(not stop) the mediaplayer in onPause, then start in onResume. That worked for me.

Answered By – Palash Mondal

people found this article helpful. What about you?