1

Well, why doesn't it?

From my understanding, finish() destroys the entire activity, so it should also destroy the running media player.

Here is my code:

MediaPlayer ourSong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ourSong = MediaPlayer.create(this, R.raw.hungat);
    ourSong.start();
    Thread timer = new Thread() {
        @Override
        public void run() {
            try {
                sleep(5000);

            } catch (InterruptedException e){
                e.printStackTrace();
            }finally {
                Intent i1 = new Intent("android.intent.action.MAINACTIVITY");
                startActivity(i1);
            }
        }
    };
    timer.start();
}
@Override
protected void onPause() {
    super.onPause();
    finish();
}

}

3 Answers3

0

When you call finish() it won't run until control is returned to Android. Since you are not stopping the MediaPlayer instance, control is not passed.

Stop and release it prior to finishing your activity, and include onDestroy() so that its disposed-of properly if activity closes.

For more information, check the official API.

MediaPlayer ourSong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ourSong = MediaPlayer.create(this, R.raw.hungat);
    ourSong.start();

    Thread timer = new Thread() {
        @Override
        public void run() {
            try {
                sleep(5000);
            } catch (InterruptedException e){
                e.printStackTrace();
                destroyMediaPlayer();
            }finally {
                Intent i1 = new Intent("android.intent.action.MAINACTIVITY");
                startActivity(i1);
            }
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    super.onPause();
    destroyMediaPlayer();
    finish();
}

@Override
protected void onDestroy() {
    super.onPause();
    destroyMediaPlayer();
    finish();
}

private void destroyMediaPlayer(){
    if(ourSong != null){
      ourSong.stop();
      ourSong.release();
    }
}
Dayan
  • 7,634
  • 11
  • 49
  • 76
  • maybe...Edit your answer again and replace mediaPlayer by ourSong...Your code do not compile with provided example – AlexTa Jun 27 '17 at 19:31
  • Why do you have to use stop and release? Can't you just use release? – Primac Antonie Jun 27 '17 at 19:39
  • @PrimacAntonie Technically you don't need to call stop prior to release, API states that release can be called on `any` state. – Dayan Jun 27 '17 at 19:43
0

The MediaPlayer is not connected to the activity and has no idea that the activity is being destroyed. Hence, the MediaPlayer is not affected by the activity being destroyed and continues doing whatever you told it to do.

In part, that is because a MediaPlayer does not need an activity -- many times, a MediaPlayer is managed by a service, because media playback should proceed independently from what the user does with the UI.

You are welcome to arrange yourself to stop the MediaPlayer when the activity is destroyed — as others noted, stop() and release() the MediaPlayer in onDestroy(). However, you have to do that yourself, as otherwise MediaPlayer has no way to know that it is supposed to stop.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Because it is a thread

use this
runnable.terminate(); thread.join();

refer this thread