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

后端 未结 2 1869
情歌与酒
情歌与酒 2021-01-03 05:15

I\'ve been running into an issue with the MediaPlayer on Lollipop devices. Basically when the device screen is off (i.e. user locked the device) the playback continues, but

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 06:06

    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 (e.g. 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);                
        }
    }
    

提交回复
热议问题