Search code examples
androidaudiomedia-player

Check isPlaying() on another activity


I've started a sound in activity A and want to stop it in Activity B. How can I do it?

Here's the code I'm using to start the sound:

MediaPlayer mp = MediaPlayer.create(con, R.raw.siren1);
mp.start();

Thanks.

Edit: Here's the solution:

Activity A

static MediaPlayer mp;
.........
mp = MediaPlayer.create(con, R.raw.siren1);
mp.start();

Activity B

confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
        if (SmsReceiver.mp.isPlaying() == true) {
    SmsReceiver.mp.stop();
    }
finish();
    }
});

Solution

  • You can declare the MediaPlayer object as static and then refer to it from the other activity.