How to use wake lock for android mediaplayer? - java

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.

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

Prevent LibGDX game from completely stopping when paused

In my LibGDX game for android, if the user backs out of the game (either by pressing the home button or switching to another application) LibGDX's built in pause() method is supposed to run. Now, this is fine, and it works fine as well. My problem is that if I back out of the game to do whatever, and then rejoin the game, it has restarted the app completely (kind of like if every time you exited and rejoined in the middle of a game of Pacman your score would be zero and all the dots would be back). For my screen switching, it is necessary that the game NOT restart every time the user exits, but simply enter the corresponding state to actually simulate the 'paused' game. How do I stop LibGDX/Android from killing the game altogether upon user exit, but simply pausing it?
The libGDX application lifecycle matches the lifecycle of the Android Activity as documented in the ApplicationListener interface so you should expect the same behavior. When you press the home button while in a libGDX game then the pause method will be called, which is the same as onPause in Android. The game will go to the background but will stay in memory. However this is not guaranteed and the OS might release the games memory for other applications, there really is no way to get around this. In the case when the game comes back to the foreground and the game restarts you'll need to load the games state from when it was paused.
I've written my own article on how to save and load the game state using Json in libGDX, maybe that will be useful to you.
You should use Asset Manager to prevent it, here a good tutorial :
http://code.google.com/p/libgdx/wiki/AssetManager
the official doc :
http://code.google.com/p/libgdx/wiki/AssetManager
Load all of your textures with assets manager
I have the same problem, but I found the solution by:
Disable option in Developer options called
- Don't keep activities (Destroy every activity as soon as the user leaves it)

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

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