1

I have a problem regarding playing a sound in an activity, I have alarm manager and I use media player to play sounds, now when I try to set two alarms (assume that these two has only 1 minute difference from their time) when the first alarm alarms and I don't close that activity then waiting for another minute another activity pops out then plays the sound too so there's two sounds playing in background.

I want to make the previous activity's sound stop when another alarms on front

Syrrea Lee
  • 101
  • 1
  • 1
  • 9
  • is it the same activity? If yes, put the attribute "singleTop" into your manifest for that activity and just request if mediaPlayer is playing before starting a new one.. – Opiatefuchs Oct 10 '16 at 13:44
  • @Opiatefuchs what if we have more than one activity? I've this case of more than one activity. – deejay Oct 10 '16 at 13:47
  • a temporary solution.. you can use a sound clip of 1 or less than 1 minute. so it will finish before the second alarm starts. – deejay Oct 10 '16 at 13:52
  • Yes only one activity since it's from alarm manager – Syrrea Lee Oct 10 '16 at 13:54
  • if more than one activity, you should create your custom mediaplayer class or a activity independant class where you start your media player, then you can reach it from everywhere... – Opiatefuchs Oct 10 '16 at 13:54
  • how should I put the "singleTop exactly? could you please post the code of it? – Syrrea Lee Oct 10 '16 at 13:55

1 Answers1

0

In your manifest put this attribute into activity:

<activity
    android:name=".YourActivity"
    android:launchMode="singleTop">

What the launchMode means and which possible states there are, see here in the docs: https://developer.android.com/guide/topics/manifest/activity-element.html

Then your activity will only be started once. If it is already started, it just comes to foreground and no second one comes up. For MediaPlayer:

if(mediaPlayer!=null){

   if(mediaPlayer.isPlaying()){

         mediaPlayer.stop();
         mediaPlayer.release();
    }


     mediaPlayer = MediaPlayer.create(this, R.raw.your_sound);
     mediaPlayer.start();
}

Be sure that you don´t call mediaPlayer.isPlaying() if you have released the mediaPlayer before. This will throw an illegalStateException. This is just from scratch, you have to adjust it to your needs.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • will this also override the previous activity? so the previous activity will be lost and changed by new activity? – Syrrea Lee Oct 10 '16 at 14:11
  • I think I have misunderstood your answer from the comments, I thought you were saying if I have assigned the media player sound in one activity (which means assigned in the same activity), so by means of, this shouldn't work for me since the next alarm didn't show up – Syrrea Lee Oct 10 '16 at 14:17
  • please show the part of code where you stuck. I think we´re talking past each other :) – Opiatefuchs Oct 10 '16 at 19:21
  • I used this code http://stackoverflow.com/questions/12266502/android-mediaplayer-stop-and-play – Syrrea Lee Oct 11 '16 at 09:57
  • My problem is when I don't stop the current alarm then another alarm pops out there will be two sounds playing on the background – Syrrea Lee Oct 11 '16 at 09:58
  • So please let me know: Are you starting more than one activity where a mediaplayer is started? – Opiatefuchs Oct 11 '16 at 10:47
  • Yes, I may have multiple activities – Syrrea Lee Oct 11 '16 at 11:11
  • then you shouild exclude the mediaPlayer from your activities and make a single class where you start the player from.... – Opiatefuchs Oct 11 '16 at 11:57