JMF: Cannot set media time on an unrealized controller - java

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

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 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().

Java Android: Mediaplayer fades automatically

I've figured out that the MediaPlayer on Android 4.4.2 seems to fade-in the Audiofile automatically.
I am using the MediaPlayer for playing a Sound that mustn't be faded in.
It worked well with a Smarthpone with 4.2.1, but on another Phone with 4.4.2 the Fading occurs.
I've also had a look at the SoundPool, but it misses the Feature of letting me know if the File is still playing.
Am I able to switch off the automatic fade-in or do I need to use the Soundpool and keep an eye on the length of the Track on my own?
Thanks,
VanDahken
You can try to setAudioAttributes(AudioAttributes attributes) before start playing.
atrs = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
Try to play with CONTENT_TYPE.
I have the same issue. The good thing is that it worked on a couple other Android versions, so hopefully only 4.4.2 has the problem. My first solution I discovered was to use an uncompressed music file... (.wav with data format LEI16) but the music file was way too big.
I also found that the fade-in doesn't seem to happen if you set the audio stream type to STREAM_RING but that's probably not a good idea since it's meant for phone rings.
For my app I only have one critical spot (when the app first starts) where the music has to play without the fade-in, and I was able to get a fix for that. The intro takes a few seconds before the music starts, so what I am doing is:
-prepareAsync() at the start of the intro
-then when prepared, setVolume() to 0f and start()
-then, after a small delay (for my case that ends up being a few seconds: the time it takes for the app's intro to complete), add a seek listener, and seekTo(0)
-when the seek completes, set the volume to the desired value

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.

Make sound cut itself off rather than play over itself

My 1st app is an audio sampler. A button starts an audio loop and other buttons play different sounds over the top.
If I hit a button multiple times, the sound will play over itself.
I want the last click to cut off any audio still playing from the same button. But I don't want it to interfere with any audio started from a different button.
(For a button that plays audio of someone rapping "my name is", 3 quick clicks of the same button should result in "my-my-my name is". But a loop should remain playing in the background).
I'm using SoundPool and OnTouchListeners.
Thanks.
I've never used SoundPool, but with MediaPlayer, if you use release() it should stop it. If you put it before the start(), it should stop it (if it's already playing) then start it again.
EDIT
From http://developer.android.com/reference/android/media/SoundPool.html#stop%28int%29
public final void stop (int streamID)
Since: API Level 1
Stop a playback stream. Stop the stream specified by the streamID. This is the value returned by the play() function. If the stream is playing, it will be stopped. It also releases any native resources associated with this stream. If the stream is not playing, it will have no effect.
Parameters
streamID a streamID returned by the play() function
Have you tried calling stop(streamID)?
If you look at the documentation for SoundPool, the constructor has a parameter for maxStreams. Also in the overview:
When the SoundPool object is constructed, the maxStreams parameter sets the maximum number of streams that can be played at a time from this single SoundPool. SoundPool tracks the number of active streams. If the maximum number of streams is exceeded, SoundPool will automatically stop a previously playing stream based first on priority and then by age within that priority.
You could create a separate SoundPool (separate from the loop) for this one sound and set maxStreams to 1.

Categories