My app is streaming audio fine on all devices except Nexus 5. On Nexus 5, the MediaPlayer randomly stops playing. Not sure if the changes with respect to Loudness (http://developer.android.com/about/versions/android-4.4.html#Multimedia) in 4.4 has broken something.
Is anyone else noticing this issue? Seems to be happening to some users, but I'm unable to reproduce on my own Nexus 5.
UPDATE: So I was able to reproduce the issue on my Nexus 5. It seems to actually be happening near the end of the clip. With about 1 - 5 seconds left in the clip, the OnCompletionListener.onCompletion()
method is called by MediaPlayer. This is only happening on the Nexus 5 and it's happening on some clips at random. I'm able to reproduce it almost 30% of the time. Note that, when the clip finishes early, if I try to go back and play the clip again it finishes playing the clip entirely the second time. I know Android 4.4 just got released, but hopefully someone out there can help! Thanks.
UPDATE: I've filed a bug against Android: https://code.google.com/p/android/issues/detail?id=62304
There is no API to check if MediaPlayer is paused or not. So please use any Boolean variable to check and toggle it when you paused using any button .
Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer. seekTo(0); mediaPlayer.
android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams. MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.
If the stop() method is called using Mediaplayer instance, then it needs to prepared for the next playback. The MediaPlayer can be moved to the specific time position using seekTo() method so that the MediaPlayer instance can continue playing the Audio or Video playback from that specified position.
Alright, I've found the solution. I'm not sure if this is the issue you're all facing now, but it fixes mine. Basically, Android 4.4+ introduces many new power management features and one of them includes shutting the CPU down while the screen is off. Quote from Android docs:
Because the Android system tries to conserve battery while the device is sleeping, the system tries to shut off any of the phone's features that are not necessary, including the CPU and the WiFi hardware. However, if your service is playing or streaming music, you want to prevent the system from interfering with your playback.
Hence, without a CPU wake lock the MediaPlayer loses its ability to stream properly, causing it to stop playback before the clip is complete. The solution for this is simple: add a PARTIAL_WAKE_LOCK to the MediaPlayer. As documented on Android:
mMediaPlayer = new MediaPlayer();
// ... other initialization here ...
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
I guess a bunch of us failed to see this in the docs. I don't remember seeing this, so maybe it was just added. Anyway, hopefully this fixes the issue for everyone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With