libre-tube / LibreTube

An alternative frontend for YouTube, for Android.
https://libretube.dev
GNU General Public License v3.0
8.54k stars 440 forks source link

Paused video autoplays when unlock screen after few minutes locked #5272

Closed luckkmaxx closed 6 months ago

luckkmaxx commented 10 months ago

Steps to reproduce

  1. Play something and leave the video minimized (not sure if the bug is also in other modes) and paused.
  2. Lock the screen with the app in foreground and go for a coffee (between 5-10 minutes)
  3. Go back and unlock the screen (I use fingerprint)
  4. The minimized video starts playing.

Maybe is related to this but the opposite behavior.

Expected behavior

Keep the video paused after unlock for long times

Actual behavior

It plays when the devices is locked for about >5-10mins

LibreTube version

0.20.1

Android version

13

Other details

No response

Acknowledgements

d4f5409d commented 9 months ago

When I get back into the app I get crashes like this


    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3876)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4018)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:111)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2474)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:240)
    at android.os.Looper.loop(Looper.java:351)
    at android.app.ActivityThread.main(ActivityThread.java:8427)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
Caused by: org.chromium.base.JniException: lateinit property exoPlayer has not been initialized
    at com.github.libretube.ui.fragments.PlayerFragment.onDestroy(SourceFile:148)
    at androidx.fragment.app.Fragment.performDestroy(SourceFile:20)
    at androidx.fragment.app.FragmentStateManager.destroy(SourceFile:125)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(SourceFile:370)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(SourceFile:1574)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(SourceFile:82)
    at androidx.fragment.app.FragmentManager.execSingleAction(SourceFile:32)
    at com.github.libretube.helpers.NavigationHelper.navigateVideo$default(SourceFile:171)
    at com.github.libretube.ui.activities.MainActivity.loadIntentData(SourceFile:204)
    at com.github.libretube.ui.activities.MainActivity.onCreate(SourceFile:866)
    at android.app.Activity.performCreate(Activity.java:8422)
    at android.app.Activity.performCreate(Activity.java:8395)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1403)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3849)
    ... 12 more
luckkmaxx commented 9 months ago

When I get back into the app I get crashes like this

  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3876)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4018)
  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:111)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2474)
  at android.os.Handler.dispatchMessage(Handler.java:106)
  at android.os.Looper.loopOnce(Looper.java:240)
  at android.os.Looper.loop(Looper.java:351)
  at android.app.ActivityThread.main(ActivityThread.java:8427)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
Caused by: org.chromium.base.JniException: lateinit property exoPlayer has not been initialized
  at com.github.libretube.ui.fragments.PlayerFragment.onDestroy(SourceFile:148)
  at androidx.fragment.app.Fragment.performDestroy(SourceFile:20)
  at androidx.fragment.app.FragmentStateManager.destroy(SourceFile:125)
  at androidx.fragment.app.FragmentStateManager.moveToExpectedState(SourceFile:370)
  at androidx.fragment.app.FragmentManager.executeOpsTogether(SourceFile:1574)
  at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(SourceFile:82)
  at androidx.fragment.app.FragmentManager.execSingleAction(SourceFile:32)
  at com.github.libretube.helpers.NavigationHelper.navigateVideo$default(SourceFile:171)
  at com.github.libretube.ui.activities.MainActivity.loadIntentData(SourceFile:204)
  at com.github.libretube.ui.activities.MainActivity.onCreate(SourceFile:866)
  at android.app.Activity.performCreate(Activity.java:8422)
  at android.app.Activity.performCreate(Activity.java:8395)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1403)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3849)
  ... 12 more

I got errors sometimes but cant reproduce it always, so didnt posted any log here

newhinton commented 8 months ago

This sounds like it is the same issue as this: #3187

Is it possible, that onResume triggers recreation of the player with the last video, and then starting to play the video, as if the user clicked on one?

https://github.com/libre-tube/LibreTube/blob/ed13edffec46ae369e7b801b472bfffa9115f099/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt#L784

newhinton commented 8 months ago

Further investigation leads me to the following assumption:

Android does something to libretube, which results in the app not beeing "fully" recreated during the next start. It keeps the backstack and the normal livecycle, so it seems to be continuously alive, but it was infact discarded at some point, in some way.

What i observed was that onViewCreated from the PlayerFragment was called, and that function calls playVideo()

So the auto-play is somewhat intended, though i think the livecycle behaves somewhat weirdly. I dont know if this is device/os-specific, but i am using grapheneOS.

I will remove the playVideo-call and report if that fixes it for me, aswell as any side-effects.

newhinton commented 8 months ago

It seems the actual culprit is this:

https://github.com/libre-tube/LibreTube/blob/7d7862873bc2171bb278484501ee6e5b2c7d7b2a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt#L936

setting this to false fixed the autoplay for me. The downside is that videos only start playing after manually pressing the play-button, but i have not really looked into writing a workaround for that.

gojzdar commented 8 months ago

Bit late but this also happened to me a few times.

First time: ~ a month ago I think Last time: today, with 0.21.0

Orientation: portrait and not miniplayer Background playback: enabled Battery optimizations: enabled*

JPT77 commented 6 months ago

I experience this issue too, with version 0.21.1 I find it totally annoying. It happens in different circumstances. For example when starting the app ist starts playing. or when unlocking the screen.

How can I apply the above workaround by @newhinton on my phone?

Bnyro commented 6 months ago

Thanks for the investigations @newhinton :+1:

I hope that #5697 fixed this issue (I couldn't test it because my phone only kills the app after a very long time), so please feel free to report back if this improved the situation (in theory it should, but it's Android, so who knows ...)