MediaPlayer randomly stops on Android 4.4 (19)

前端 未结 3 1232
我寻月下人不归
我寻月下人不归 2020-12-31 11:39

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://d

相关标签:
3条回答
  • 2020-12-31 12:14

    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!

    0 讨论(0)
  • 2020-12-31 12:17

    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
    0 讨论(0)
  • 2020-12-31 12:20

    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:

    • https://android.googlesource.com/platform/frameworks/av/+/7fa0152
    • Restore NuPlayer error and EOS handling This was erroneously removed by commit a73c954
    0 讨论(0)
提交回复
热议问题