kohii icon indicating copy to clipboard operation
kohii copied to clipboard

State dose not change of previous video

Open falguni0314 opened this issue 4 years ago • 33 comments

Hi, thanks for kohii, love using it but I am facing below issue

If I pause certain video in view pager and move to next video and again go back to previous video it's still in pause state, kindly help me how to change the state and auto play the previous video.

falguni0314 avatar Aug 14 '20 15:08 falguni0314

Can you share with me the piece of code you setup the ViewPager and Video? Basically a simple way for me to reproduce the issue so I can take a look into it.

eneim avatar Aug 16 '20 12:08 eneim

@eneim thanks for your response, we have figured out the problem and fixed it and works.

We are facing below two problem, it would be great if you could help us to fix this

  1. when app is in onPause state video is still playing in background

  2. we are not able to get the state of exo player when it's buffering

falguni0314 avatar Aug 16 '20 12:08 falguni0314

Hi. About 1, can you try the "activeLifecycleState" flag when you setup a Manager? Try to set it to RESUMED and see if it helps.

About 2, you can use addStateListener to the Playback when get it from the callback in your setup: https://github.com/eneim/kohii/blob/c99beece0965d0b25f8ab754712f4bdb1e93cbab/kohii-sample/src/main/java/kohii/v1/sample/ui/combo/ExoVideosAdapter.kt#L77

Please try this and let me know if it helps.

eneim avatar Aug 16 '20 12:08 eneim

  1. activeLifecycleState = Lifecycle.State.RESUMED it's already set to RESUEMED

  2. it.addStateListener(holder) added it's extending Playback.StateListener, could you please help me where are the methods for this

falguni0314 avatar Aug 16 '20 13:08 falguni0314

About 1: if so it is a bug, I will take a look. Please tell me it is after onPause is called then the Video keeps playing? It is a Fragment or Activity?

About 2: in that call I let the ViewHolder implement the interface. You can use anything. The method addStateListener is set for a Playback object. In the call to "bind" you have a callback that notifies when the Playback is available. From there you can call addStateListener.

eneim avatar Aug 16 '20 13:08 eneim

  1. It's an activity, kindly let us know once you are done with your investigation.

  2. Thanks for your response.

falguni0314 avatar Aug 16 '20 13:08 falguni0314

@falguni0314 Tried to use the TikTok demo for infestigating the issue 1, but having no luck. Is it possible for you to provide a simple project to reproduce it?

eneim avatar Aug 17 '20 03:08 eneim

Sure, below is the sample project attached.

DemoPlayer.zip

falguni0314 avatar Aug 17 '20 03:08 falguni0314

@falguni0314 Thanks for the code. How did you produce the onPause event? The code seems to work for me though.

debug_kohii_demo_player

Below is your code with some minor change to debug: DemoPlayer_debug.zip

eneim avatar Aug 17 '20 05:08 eneim

@eneim thanks for your response the problem is when app is running and video is playing then if we press home button video plays in background, we want to stop that, but while playing the video if we turn off the screen it pauses the video which is ok.

falguni0314 avatar Aug 17 '20 05:08 falguni0314

I see. Please help me to clarify the use cases:

  • Press home while the app is on foreground and Videos are playing --> Videos should stop playing? Question: Will it be Paused, meaning that when you open it from the background, it starts from where it was paused, OR it will be Stopped, meaning that when you open it from the background, it starts from the beginning?

  • Turn off the screen while the app is on foreground and Videos are playing --> Videos should stop playing? Question: Will it be Paused, meaning that when you open it from the background, it starts from where it was paused, OR it will be Stopped, meaning that when you open the screen again, Video starts from the beginning?

eneim avatar Aug 17 '20 06:08 eneim

  1. Yes video should stop playing a. Yes it should pause and should starts from where it was paused

  2. Yes video should stop playing a. Yes it should pause and should starts from where it was paused

falguni0314 avatar Aug 17 '20 06:08 falguni0314

Hmm, I think it is working though. Below is a test using Android Emulator SDK 27.

debug_kohii_demo_player_2

Key event 3 = press home Key event 26 = press power (turn screen on/off)

Can you tell me your testing environment? - Android device model if it is real device, or Emulator base image, Android SDK version where you reproduce the issue? The exact actions you did to reproduce the issue and what you observe that makes you believe it is misbehaving?

eneim avatar Aug 17 '20 06:08 eneim

I am using real device, below is device details

Device name: Oneplus 7T pro OS: Android 10 SDK: 30

Action 1: Video is playing in foreground and then home button pressed app goes to background but video still plays in background - PROBLEM

Action 2. Video is playing in foreground and then power button pressed and screen goes off video pauses, again when screen is on video resumes - NOT A PROBLEM

falguni0314 avatar Aug 17 '20 06:08 falguni0314

Got it. Let me see if I can address this issue. Can you try the source code I sent above and check the onStateChanged callback and tell me what is the event sent to the callback when you press the home button? Maybe the Lifecycle reports wrong result.

eneim avatar Aug 17 '20 06:08 eneim

Once home button pressed I am getting below log

lifecycle state: STARTED lifecycle state: CREATED

Then again when app comes to foreground getting below log

lifecycle state: STARTED lifecycle state: RESUMED

Also just an observation

When power button pressed getting below exact log

2020-08-17 12:26:44.991 26586-26586/com.nubesly.demoplayer D/Demo: lifecycle state: STARTED
2020-08-17 12:26:45.011 26586-26586/com.nubesly.demoplayer D/Demo: lifecycle state: CREATED
2020-08-17 12:26:45.027 26586-26648/com.nubesly.demoplayer D/SurfaceUtils: connecting to surface 0x769d172010, reason connectToSurface
2020-08-17 12:26:45.027 26586-26648/com.nubesly.demoplayer I/MediaCodec: [OMX.qcom.video.decoder.avc] setting surface generation to 27224067
2020-08-17 12:26:45.027 26586-26648/com.nubesly.demoplayer D/SurfaceUtils: disconnecting from surface 0x769d172010, reason connectToSurface(reconnect)
2020-08-17 12:26:45.027 26586-26648/com.nubesly.demoplayer D/SurfaceUtils: connecting to surface 0x769d172010, reason connectToSurface(reconnect)
2020-08-17 12:26:45.031 26586-26650/com.nubesly.demoplayer D/SurfaceUtils: set up nativeWindow 0x769d172010 for 640x368, color 0x7fa30c06, rotation 0, usage 0x20002900
2020-08-17 12:26:45.031 26586-26648/com.nubesly.demoplayer D/SurfaceUtils: disconnecting from surface 0x769d150010, reason disconnectFromSurface
2020-08-17 12:26:45.114 26586-26586/com.nubesly.demoplayer I/Demo: paused https://multiplatform-f.akamaihd.net/i/multi/will/bunny/big_buck_bunny_,640x360_400,640x360_700,640x360_1000,950x540_1500,.f4v.csmil/master.m3u8+0
2020-08-17 12:26:45.162 26586-26638/com.nubesly.demoplayer D/AudioTrack: ClientUid 10340 AudioTrack::pause 

When home button pressed getting below exact log

2020-08-17 12:27:34.677 26867-26867/com.nubesly.demoplayer D/Demo: lifecycle state: STARTED
2020-08-17 12:27:34.684 26867-26867/com.nubesly.demoplayer I/Surface: opservice is null false
2020-08-17 12:27:34.710 26867-26930/com.nubesly.demoplayer D/SurfaceUtils: connecting to surface 0x768c1b7010, reason connectToSurface
2020-08-17 12:27:34.710 26867-26930/com.nubesly.demoplayer I/MediaCodec: [OMX.qcom.video.decoder.avc] setting surface generation to 27511811
2020-08-17 12:27:34.710 26867-26930/com.nubesly.demoplayer D/SurfaceUtils: disconnecting from surface 0x768c1b7010, reason connectToSurface(reconnect)
2020-08-17 12:27:34.710 26867-26930/com.nubesly.demoplayer D/SurfaceUtils: connecting to surface 0x768c1b7010, reason connectToSurface(reconnect)
2020-08-17 12:27:34.711 26867-26932/com.nubesly.demoplayer D/SurfaceUtils: set up nativeWindow 0x768c1b7010 for 640x368, color 0x7fa30c06, rotation 0, usage 0x20002900
2020-08-17 12:27:34.712 26867-26930/com.nubesly.demoplayer D/SurfaceUtils: disconnecting from surface 0x76fbf1e010, reason disconnectFromSurface
2020-08-17 12:27:34.744 26867-26867/com.nubesly.demoplayer D/Demo: lifecycle state: CREATED

falguni0314 avatar Aug 17 '20 06:08 falguni0314

Got it. Thanks. Currently I cannot reproduce the issue. I will go through the code and see what I can do. At the meantime, please try the code below to manually lock/unlock the playbacks in a Manager:

class MainActivity : AppCompatActivity(), LifecycleEventObserver {
    private var pager: ViewPager2? = null
    var kohii: Kohii? = null
    
    lateinit var manager: Manager
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        kohii = KohiiProvider.get(this)

        pager = findViewById(R.id.container)
        manager = kohii!!.register(
            this,
            memoryMode = MemoryMode.HIGH,
            activeLifecycleState = Lifecycle.State.RESUMED
        )
            .addBucket(pager!!)

        val adapter = VideoClipAdapter(kohii!!)
        pager!!.adapter = adapter
        pager!!.offscreenPageLimit = 1

        lifecycle.addObserver(this)
    }

    // You can use onPause and onResume instead.
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        Log.d("Demo", "lifecycle state: ${source.lifecycle.currentState}")
        if (!source.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
            kohii?.lockManager(manager)
        } else {
            kohii?.unlockBucket(manager)
        }
    }
}

eneim avatar Aug 17 '20 07:08 eneim

Also, if you have a few minute, please use a checkpoint at this line https://github.com/eneim/kohii/blob/1d8e8b51f8905aee5d9ee82df022f98e666d00a5/kohii-core/src/main/java/kohii/v1/core/Manager.kt#L297 and see when you press the home button, it goes to that line or not. It can be some device-specific issue that only you can help.

eneim avatar Aug 17 '20 07:08 eneim

kohii?.unlockBucket(manager)

manager gives below error

Type mismatch: inferred type is Manager but View was expected

falguni0314 avatar Aug 17 '20 07:08 falguni0314

Ahhhh sorry, it should be unlockManager. A bad copy-paste.

eneim avatar Aug 17 '20 07:08 eneim

The above provided solution is not working, but interesting finds I have made which are below based on devices

  1. Nokia 7plus - Works fine
  2. Samsung Note 9 - Work fine
  3. Xiomi Mi A3 - Works fine
  4. Motorola Moto X4 - Works fine
  5. Oneplus 7T pro - Not Working

falguni0314 avatar Aug 17 '20 07:08 falguni0314

@falguni0314 Interesting result. Does the other devices you got above work without the change I suggest?

eneim avatar Aug 17 '20 08:08 eneim

It's working without the change.

falguni0314 avatar Aug 17 '20 08:08 falguni0314

I see. If so it might be something lies inside the flow. The only thing I can think of is Oneplus 7T has custom Handler implementation that may not deliver the messages correctly.

eneim avatar Aug 17 '20 08:08 eneim

Ok, is there any work around for this ?

falguni0314 avatar Aug 17 '20 08:08 falguni0314

@falguni0314 I'm afraid not yet. There is Master.lock() which is the kill-switch for every Playback in the library (you can get the master by calling kohii.master), but it has the same effect as kohii.lockManager above: Currently this is the condition I use to decide to return empty candidates or not:

lock || lifecycleOwner.lifecycle.currentState < activeLifecycleState

So basically the workaround above covers what can be done already. Unless I can pinpoint the root cause, I cannot sure any other practice can help. Of course, you can try calling kohii?.master?.lock() but I doubt it works.

eneim avatar Aug 17 '20 12:08 eneim

@eneim I have made an amazing find that writing below code fixes all the problem

override fun onStop() {
    super.onStop()
    kohii?.master?.unlock()
}

falguni0314 avatar Aug 17 '20 14:08 falguni0314

lol this is so unexpected >.< does it work with unlockManager? basically you should control the scope you want to unlock.

Maybe the way the messahe queue works needs some investigations.

eneim avatar Aug 17 '20 14:08 eneim

No unlock manager is not working

kohii?.unlockManager(manager) - Not working

kohii?.master?.unlock() - Working

falguni0314 avatar Aug 17 '20 14:08 falguni0314

Also by doing the above code creates a new bug, if we turn off the display and then again turn it on, video doesn't play, shows a blank screen.

falguni0314 avatar Aug 17 '20 14:08 falguni0314