How to tell when Mediaplayer has finished playing audio - java

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

Related

Android media player stop playing in service

I already asked about it before, but my question even after edits about my progress of fix this noone answered to it anymore. This is original question:
Android media player stop playing while in background
So in short, I'm making music player app, but mediaPlayer stop sometimes when loading next song. After many tests I find out that it stops at mediaPlayer.prepare() and it won't continue as long as I don't trigger any action on phone like turning on display, volume up/down, click headset button. I'm out of ideas what can be a reason of it.
maybe you need to call prepareasync() instead of prepare().

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 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

How to use wake lock for android mediaplayer?

I'm trying to play a playlist with the android media player. When one track ends; i stop the media player and init it again with the new one. Everytime i init the player I set the wake lock with this line;
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
For testing; i start the player and lock my phone screen. After a couple of tracks; the player stops and cannot pass to the other track. When this happens i wait a couple of minutes more. Then when i open my phone screen; the player starts immediately.
Is there any posibility that the mediaplayer releases the wakelock somehow? May be between the track switching in the playlist when it stops? Is the "MediaPlayer.setWakeMode" proper way to do it when dealing with playlists? Or should i use "PowerManager" class directy?
This problem takes too much time to test. Thx in advance.

JMF: Cannot set media time on an unrealized controller

I'm trying to use JMF to play a sound every time I hit a button.
This is what I have to contruct the media player:
MediaPlayer mp = new javax.media.bean.playerbean.MediaPlayer();
mp.setMediaLocation("file:///D:/fall.wav");
mp.setPlaybackLoop(false);
And, to play the sound once, I do
mp.start();
mp.setMediaTime(new Time(0));
mp.stop();
The first time I play the sound, i get a
javax.media.NotRealizedError: Cannot set media time on a unrealized controller
and an
Exception in thread AWT-EventQueue-0" javax.media.NotRealizedError:
Cannot set media time on a unrealized controller
on the setMediaTime line. This doesn't happen any subsequent time I hit the button that makes the sound, it only happens the first time. If i change the constructor and add
mp.realize();
at the end, then it doesn't throw those errors, but it also doesn't play the sound the first time.
How do I go about fixing this?
mp.start() is asynchronous. So when you call mp.setMediaTime(new Time(0));
immediately after calling start(), the player is not yet realized.
You could add a ControllerListener and listen to its events. Once you get the RealizedEvent, then set the media time. It will be a good idea to initially call mp.realize() and on getting the event, set the media time and then call start()

Categories