George Lungu
George Lungu

Reputation: 125

Stop mediaplayer through onDestroy

I'm trying to make an activity with 10 buttons which will play some music on click and when user taps the back button he should exit from the app and the music should stop. All that I tryied didn't made anything.

This is my code:

package app.technozed.yearstest;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ToggleButton unu = (ToggleButton)this.findViewById(R.id.button1);
    final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.tt);
    ToggleButton doi = (ToggleButton)this.findViewById(R.id.button2);
    final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.rr);
    ToggleButton trei = (ToggleButton)this.findViewById(R.id.button3);
    final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.dd);
    ToggleButton patru = (ToggleButton)this.findViewById(R.id.button4);
    final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.vv);
    ToggleButton cinci = (ToggleButton)this.findViewById(R.id.button5);
    final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.ss);
    unu.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            // If the music is playing
            if(mp1.isPlaying()) {
                // Pause the music player

                mp1.setLooping(false);
                mp1.pause(); }
            // If it's not playing
            else
            {
                // Resume the music player
                mp1.setLooping(true);
                mp1.start(); }
        }
    });
        doi.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

                // If the music is playing
                if(mp2.isPlaying() == true) {
                    mp2.setLooping(false);
                    // Pause the music player
                    mp2.pause(); }
                // If it's not playing
                    else {
                    // Resume the music player
                    mp2.setLooping(true);
                    mp2.start(); }
            }
        });
        trei.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

                // If the music is playing
                if(mp3.isPlaying()) {
                    mp3.setLooping(false);
                    // Pause the music player
                    mp3.pause(); }
                // If it's not playing
                else {
                    // Resume the music player
                    mp3.setLooping(true);
                    mp3.start(); }
            }
        });
        patru.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

                // If the music is playing
                if(mp4.isPlaying()) {
                    mp4.setLooping(false);
                    // Pause the music player
                    mp4.pause(); }
                // If it's not playing
                else {
                    // Resume the music player
                    mp4.setLooping(true);
                    mp4.start(); }
            }
        });
        cinci.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

                // If the music is playing
                if(mp5.isPlaying()) {
                    mp5.setLooping(false);
                    // Pause the music player
                    mp5.pause(); }
                // If it's not playing
                else {
                    // Resume the music player
                    mp5.setLooping(true);
                    mp5.start(); }
            }
        });
}
@Override
protected void onDestroy()
{
    super.onDestroy();
    MediaPlayer mp = new MediaPlayer();
   if (mp.isPlaying()) { mp.stop(); mp.release();
     //finally
}
}

What I understood is that in onDestroy part I'm doing it wrong, but how can I release mp1, mp2, MP3, etc.?

Upvotes: 0

Views: 2520

Answers (3)

Robin Dijkhof
Robin Dijkhof

Reputation: 19288

You are creating a new MediaPlayer. You should stop mp1, mp2, mp3, mp4 and mp5.

Personally i would do something like this on each onClick

        public void onClick(View v) {
            MediaPlayer mp = MediaPlayer.create(this, //Specified type);
            // If the music is playing
            if(mp.isPlaying() == true) {
                mp.setLooping(false);
                // Pause the music player
                mp.pause(); }
            // If it's not playing
                else {
                // Resume the music player
                mp.setLooping(true);
                mp.start(); }
        }

This will be a lot more easy because you can stop the playing with one simple line. Unfortunately you will not be able to play two different streams at the same time.

Upvotes: 0

Estel
Estel

Reputation: 2204

You should store a reference to the MediaPlayer object within the class, which you can then stop later. What you're doing at the moment is creating a new instance of MediaPlayer that doesn't play anything and attempting to stop it.

public class MainActivity extends Activity {
  private MediaPlayer mediaPlayer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    mediaPlayer = MediaPlayer.create(this, R.raw.whatever);
    ...
  }

  @Override
  protected void onDestroy() {
    ...
    if (mediaPlayer.isPlaying()) {
      mediaPlayer.stop();
    }
    ...
  }
}

Upvotes: 1

JanBo
JanBo

Reputation: 2933

You need to declare your MediaPlayers as "global" variables, to be more specific as Class fields. Like this:

    public class MainActivity extends Activity {

    public MediaPlayer mp1, mp2, mp3 .....

And instantiate them in onCreate and dispose them in onDestroy.

By declaring them this way, you can access them withing class methods.

Your way you declared them as local variables in function and thus they dont exist in other methods

Upvotes: 0

Related Questions