0

I got three questions.

  1. How can I do it that the sound of my MediaPlayer stops when i switch/close the App/Activity?

  2. How can I do it that sound1 stops when I press on sound2?

  3. How can I stop and play the sound on one button? I mean that when I press button1 once then sound starts and when I press him again the sound stops, and so on.

I already tried it but it doesn't worked.

Here is my MainActivity:

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


        Button button1;
        Button button2;
        boolean w;
        boolean b;

        private MediaPlayer mp;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1 = (Button) findViewById(R.id.button1);
            button2= (Button) findViewById(R.id.button2);
            w = true;
            b = true;
    //Button1
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(b == true){
                    mp= MediaPlayer.create(MainActivity.this, R.raw.song1);
                    mp.start();
                        b = false;
                }else{
                        mp.stop();
                        b = true;
                    }

                }
            });
    //Button 2
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(w == true){

                        mp= MediaPlayer.create(MainActivity.this, R.raw.sound2);
                        mp.start();
                        w = false;
                    }else{
                        mp.stop();
                        w = true;

                }}
            });
        }
    //Stopping the sound when switching or closing the App
        @Override
        public void onPause() {
            mp.stop();
            super.onPause();
        }



}

Can you tell me how to solve my problems?

I'm new on Android Studio and sorry for my bad English :)

Aaron Waller
  • 175
  • 4
  • 21
  • 1
    What is w or b!? Are you even going to know the answer yourself in 6 months time? Why not [isPlaying](https://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying())? – Nick Cardoso Feb 25 '17 at 21:52

2 Answers2

0

The answer seems quite simple: keep a reference to your media player. As it currently is, you create a new MediaPlayer each time. But you should also be careful about MediaPlayer state, it's quite picky.

Take this Example flow:

  • Start app > [ mp=null, b=false, w=false ]
  • Click button 1 > [ mp=instance1, b=true, w=false ] > [ instance1 is playing ]
  • Click button 2 > [ mp=instance2, b=true, w=true] > [ instance1 is playing, instance2 is playing ]
  • Click button 1 > [ mp=instance2, b=false, w=true] > [ instance1 is playing, instance2 is stopped]
  • Pause App > [ mp=instance2, b=false, w=true] > [ instance1 is playing, instance2 is stopped]

It's also important to release() your media player when you pause

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
0

Refer to this article first https://developer.android.com/guide/topics/media/mediaplayer.html#mediaplayer

1.

public void onDestroy() {
     if (mp!=null) {
          mp.stop();
          mp.release();
     }
}

2.

//Stop media player
mp.stop();
// Set data source of sound 2
mp.setDataSource(url2);
mp.prepare(); 
mp.start();

3.

//Pause the sound
mp.pause();
//Resume the sound
mp.start();
SergGr
  • 23,570
  • 2
  • 30
  • 51
albeee
  • 1,452
  • 1
  • 12
  • 20