11

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

b.lyte
  • 6,518
  • 4
  • 40
  • 51

3 Answers3

6

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!

b.lyte
  • 6,518
  • 4
  • 40
  • 51
  • 4
    This seems to be happening on Android 5.0 again even though I have the above mentioned locks acquired. Trying to figure out why this is happening. – b.lyte Jan 05 '15 at 23:59
  • Hey, clu ..have you find the solution of this problem because i am also facing this same problem in android 5.0 (Nexus 7)...any help will be appreciated. – sid Mar 27 '15 at 04:58
  • did u find any solutions for 5.0+ ? @clu – donmezburak Oct 09 '15 at 13:30
  • Hey all. Sorry for delayed response. I've actually stopped development on Android using the MediaPlayer so I'm not current with this issue. One thing I remember running into though was that Android 5+ was using an experimental media player. I believe this can be unset in the Developer Options of the phone. I remember we had to email our users to let them know to disable the experimental media player. Terrible solution I know, but that's what I recall. Hope it helps. – b.lyte Oct 09 '15 at 18:29
  • Check this if the above answer haven't solved your problem: https://stackoverflow.com/questions/6241687/mediaplayer-stop-playing-after-about-5-seconds – Moz Mar 27 '23 at 15:47
1

Run into almost the same problem recently: MediaPlayer works perfectly on Android 4.3 and below, but fails to play the same videos on Android 4.4.

Decided to switch to vitamio library and now my app works on 4.4 as well. vitamio API is identical to the MediaPlayer's one, so the migration was quite easy.

But this solution still has some drawbacks:

  • You have to buy a license if you are not an individual developer
  • The app size will be increased ~11 megabytes
Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
1

This problem is possible related to bug: http://code.google.com/p/android/issues/detail?id=63032

The problem associated with the above bug is fixed in 4.4.1/4.4.2. The change log entry that is suspected to be the issue has the following information:

Atomicblah
  • 69
  • 4