Android MediaPlayer Handler for when Music Stops - java

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

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
});

Stop playing the previous sound when another sound plays

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.

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.

Reset Entire Activity in Eclipse

I need help to reset an entire activity after i am done with it, so that when I comeback to it, everything is back to it's original setting.
So, I have an activity named ColorGame and Scoreboard. What I want to happen is to after I finish playing in ColorGame Activity, it will go to Scoreboard (which is working fine). Then in Scoreboard Activity, I have a button that either will go to the main menu or to go back and play again in ColorGame Activity. I have an intent from Scoreboard to the ColorGame activity, however, everytime I click the PLAYAGAIN button to return to play the game again, the points and time is still the same number from the previous game. Could you guys help me?
I have these codes. This is my code for ColorGame to ScoreBoard:
Intent goScoreBoard = new Intent(ColorGame.this, ScoreBoard.class);
goScoreBoard.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(goScoreBoard);
and this is my codes for ScoreBoard to ColorGame:
Intent playAgain = new Intent(ScoreBoard.this, ColorGame.class);
playAgain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(playAgain);
It seems that you're using single top launch mode for the Activity. You should implement onNewIntent in ColorGame and reset all the data in the Activity there.

How can I make background music for android project

I'm making a small android app like image below:
I have a sound button. All I want is background music will play across all activity and when I click on sound button, music will stop. Or click to play music again.
I dont know how to make it. PLease help me! or give me link to refer.
I'd gladly appreciate your help it Thanks!
Hi please Follow below steps
initialized
MusicPlayer object when your app starts MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
set this in your button's click event.
Start it by mediaPlayer.start();
3.Stop it by mediaPlayer.stop();
but when you stopping mediaPlayer before that just check mediaPlayer.isPlaying() with if Condition. then Stop. else error thrown.
or follow this Example Turorial Link
In order for media to be played in the background of your app location when the user is interacting with it you must start a Service from your application's main activity and the service shall contain all the methods related to playback. To allow activity to interact with the service, a service connection is also required. In short, we need to implement a bounded service.
Refer http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App

Categories