Stop playing the previous sound when another sound plays - java

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

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.

Related

How to tell when Mediaplayer has finished playing audio

I have a boolean for when the music stops playing,
mediaPlayer.getStatus().equals(Status.PLAYING);
The issue is, when the song finishes playing, it still thinks its playing. On android there is a OnCompletionListener(). I was wondering if there was something similar on PC.
I'm using the Mediaplayer from JavaFX
You can use onEndOfMedia() hook.
Event handler invoked when the player currentTime reaches a media marker.
player.setOnEndOfMedia(() -> {
// Your logic here
});

android - MediaPlayer stops working at some point

I have been working on an app, and it involves sound.
Now, I use this to start it:
final MediaPlayer tick = MediaPlayer.create(getApplicationContext(), R.raw.tick_16);
tick.start();
But for some reason, after some time (depends on the device, on HTC One it is a few seconds and in others a few minuets) all sound stopps.
What is the cause of it and how can I solve it?
Use this tutorial to play audio :
http://www.tutorialspoint.com/android/android_mediaplayer.htm
you may be use tick.start(); in onResume() method of your activity class
Try starting the MediaPlayer in a Service and a Notification inside startForeground().

Android - onPause / onWindowFocusChanged are not called when entering a notification

I have a MediaPlayer which runs from my main activity. I stop it on onPause.
The problem is that when I click on something from the notification bar (for example, a whatsapp message), onPause is not called and I can't stop the music.
I tried stopping it using onWindowFocusChanged, but that as well is not being called when entering an app from the bar.
How can I stop the music in this situation?
Thanks.

Android MediaPlayer Handler for when Music Stops

I need to know when the MediaPlayer stops playing the music I request, so I can invoke a new Activity to Start.
In my application, I have buttons that Speak when I click them, but I want to listen the entire audio and only after I invoke the start of the next activity.
I am doing this way now:
MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.h);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();
Intent intent = new Intent(this, DrawActivity.class);
startActivity(intent);
But the music doesn't even start, I want the new Intent to start only when I the music stops. I know that I can make a postDelayed Runnable to launch the activity, but I would not know the exact time, only a guess...
Is there a way to attach an Event Handler to the MediaPlayer to when the music ends, it invokes the method associated to the Event Handler?
That would be amazing.
Thanks alot for your help anyway ;)
Check this callback. But this is called when end of playback source was reached. Also You can check other callbacks there

The sound stopped when press lock button

I had the Android App which play sound when specific times , I did my
code well but when I locked the screen the sound isnot play , I
checked my code and I find that the code that I added when user press
lock button is the reason of the problem .How to solve this issue ?
#Override
protected void onPause() {
Player.stopAzan();
finish();
super.onPause();
}
Maybe try using a service? Services are basically same that activities but they run in background and have no content view if I get them right :D
http://www.vogella.com/tutorials/AndroidServices/article.html
What you are trying to achieve is impossible since when the screen is locked, the Activity is stopped.
You either decide if want to play in background or not, because when the screen is locked, the Activity goes background.
If you don't need to play in background then you're good to go, just remove the finish() method.
If you do need to play the music in background, use a Service to start and stop player based on Intents passed from Activity user controls.

Categories