简体   繁体   中英

MediaPlayer cutting off playback too early on Lollipop when Screen is off

I've been running into an issue with the MediaPlayer on Lollipop devices. Basically when the device screen is off (ie user locked the device) the playback continues, but ends about 1 - 2 seconds too early. This doesn't happen when the screen is on though.

I have an onCompletionListener on the MediaPlayer:

@Override
public void onCompletion(final MediaPlayer mediaPlayer) {
    int progress = mediaPlayer.getCurrentPosition();
    int duration = mediaPlayer.getDuration();
    Log.d("PlaybackController", "progress: " + progress + " duration: " + duration);
    Log.d("PlaybackController", "Delay: " + (duration - progress)); // I'm seeing a difference of 1 - 3 seconds :(.
    mServiceHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            broadcastCompleted();
        }
    }, Math.max(duration - progress, 0));
}

This usually prints: Delay: [1500 - 3000] . I was wondering if there was a wake lock I'm missing, but I'm making the correct locks mentioned here: http://developer.android.com/guide/topics/media/mediaplayer.html , which include a PARTIAL_WAKE_LOCK and a WifiLock. Is there something else I'm missing?

Ok it looks like the issue is Android 5.0.1's experimental MediaPlayer called NuPlayer. NuPlayer is being enabled by default on all Android 5.0.1 devices and is only disabled through Developer Options. I've filed a bug against Android here: https://code.google.com/p/android/issues/detail?id=94069&thanks=94069&ts=1420659450

Here's a sample email you can send your users when they face issues with Media playback on Android 5.0.1 devices:

It looks like this might be a bug on Android's new experimental MediaPlayer called NuPlayer. To fix this, please follow these steps:

  1. Go to Android Settings
  2. Go to "About Phone"
  3. Scroll down to "Build Number" and tap the Build number 7 times.
    • You'll see a message saying "you are now X steps away from being a developer".
    • After tapping it 7 times it will say "You are now a developer!"
  4. Go back to the main settings screen and you'll see a new option called "Developer Options" right above "About Phone"
  5. Go into Developer Options and Unselect "Use NuPlayer (experimental)" under the Media section.

Update : Setting a partial wake lock on the MediaPlayer resolves this problem:

playerToPrepare.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);

A partial wake lock shouldn't have too big of an impact, and it seems like MediaPlayer itself cleans this up when playback completes.

-- Original Answer ---

I'm cross-posting my answer from here Prevent my audio app using NuPlayer on Android Lollipop 5.x? , until there's a fix out for NuPlayer, the best you can do is to detect when NuPlayer is enabled and take the user to the Developer Settings.

This approach checks Android's system properties values to see if the user have enabled the use of AwesomePlayer or not under Developer Settings. Since Lollipop have NuPlayer on by default, if this value is disabled, we know NuPlayer will be used.

Drop SystemProperties.java into your project for access to read the system properties, do not change its package name from android.os (it calls through to its corresponding JNI methods, so needs to stay the same).

You can now check if the phone is Lollipop/5.0, if AwesomePlayer is enabled, and act accordingly if it's not (eg by opening the Developer Settings):

public void openDeveloperSettingsIfAwesomePlayerNotActivated(final Context context) {
    final boolean useAwesome = SystemProperties.getBoolean("persist.sys.media.use-awesome", false);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !useAwesome) {
        final Intent intent = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
        context.startActivity(intent);                
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM