eneim / kohii

Android Video Playback made easy.
https://eneim.github.io/kohii/
Apache License 2.0
375 stars 50 forks source link

Manual playback and Visibility stop #112

Open loicsiret opened 3 years ago

loicsiret commented 3 years ago

Hi,

I face a problem when I have two video on my recyclerview. I Implemented a onclick listener to manual start/pause of my playback.

When I click on the first one, everything is fine it pause/start as required. But on the second one, playback.manager.play(playable) is called, but it didn't play.

If I set kohiiCanPause to false in my controller, my click listener works, but the video did'n't pause when the visibility threshold is reached.

Here is my code

fun setUp(listener: VideoViewHolder, videoObjectModel: VideoObjectModel, position: Int, prefix: String): Binder {

        val mediaAd = AdMediaItem(
            videoObjectModel.url.toUri(),
            customTargetingHelper.buildsAdMediaUri(videoObjectModel.adTagUrl.orEmpty())
        )

        return manilo.setUp(mediaAd) {
            tag = "$prefix-${videoObjectModel.url}-$position"
            artworkHintListener = listener
            repeatMode = if (videoObjectModel.loop) Common.REPEAT_MODE_ALL else Common.REPEAT_MODE_OFF
            threshold = THRESHOLD
            controller = object : Controller {
                override fun kohiiCanStart(): Boolean = videoObjectModel.autoplay
                override fun kohiiCanPause(): Boolean = false

                override fun setupRenderer(
                    playback: Playback,
                    renderer: Any?
                ) {
                    if (renderer is PlayerView) {
                        renderer.setControlDispatcher(manilo.createControlDispatcher(playback))
                    }
                    listener.binding.playerView.videoSurfaceView?.setOnClickListener {
                        switchPlayPause(playback)
                    }
                    listener.binding.videoOverlaycontainer.videoOverlaycontainer.setOnClickListener {
                        switchPlayPause(playback)
                    }
                }

                fun switchPlayPause(playback: Playback) {
                    playback.playable?.let { playable ->
                        if (playable.isPlaying()) playback.manager.pause(playable)
                        else playback.manager.play(playable)
                    }
                }

                override fun teardownRenderer(
                    playback: Playback,
                    renderer: Any?
                ) {
                    if (renderer is PlayerView) {
                        renderer.setControlDispatcher(null)
                    }
                }
            }
        }
    }
loicsiret commented 3 years ago

Hi,

Any help on this please ?

eneim commented 3 years ago

@loicsiret Sorry I could just get back to the library lately.

But on the second one, playback.manager.play(playable) is called, but it didn't play.

Is the second one the only Video in the screen, or the first video is being played at the same time?

eneim commented 3 years ago

If I set kohiiCanPause to false in my controller, my click listener works, but the video did'n't pause when the visibility threshold is reached.

This is expected because Kohii cannot pause the Video automatically if you say so.

loicsiret commented 3 years ago

Hi @eneim

The two video are visible at the same time

loicsiret commented 3 years ago

I ve got some "better" result, but not perfect with this :

override fun kohiiCanPause(): Boolean = kohiiAutomaticPlay

private fun Playback.forcePlay() {
      playable?.let { playable ->
            // this is needed to force Kohii to consider this as a manual play
            kohiiAutomaticPlay = false
            manager.play(playable)
            // reset flag to activate auto pause/play
            kohiiAutomaticPlay = true
      }
 }

To my knowledge, why looking kohiiCanPause when you checking if it's a manual played ?

eneim commented 3 years ago

@loicsiret Let me pull something together to better understand your requirement:

Say you have 2 Videos vertically: A above B, both are fully visible to the user (so they have the same chance of being played if allowed).

Assuming that, no scroll is taken place, which means our Videos' positions never change. Let's consider the scenarios below:

  1. When A is playing, B is paused, you manually start B -> what is the expected playback state of A and B?
  2. When A is paused (in this case, by a manual pause), B is paused, you manually start B -> what is the expected playback state of A and B?
  3. When A is paused (in this case, by a manual pause), B is playing (maybe by a manual start), you manually start A -> what is the expected playback state of A and B?

I have an experiment now, but I need your input to see if it can fit the existing spec or not.

eneim commented 3 years ago

To my knowledge, why looking kohiiCanPause when you checking if it's a manual played ?

This flag allows a Video to be manually started, and not be paused automatically (so even if it is scrolled off the screen, it can be kept playing). I actually do not encourage the scenario, but it is kinda a scenario someone might ask for, and it is the responsibility of a library to cover that possible scenario though ...

loicsiret commented 3 years ago

Assuming that, no scroll is taken place, which means our Videos' positions never change. Let's consider the scenarios below:

When A is playing, B is paused, you manually start B -> what is the expected playback state of A and B? -> A should pause and B start playing When A is paused (in this case, by a manual pause), B is paused, you manually start B -> what is the expected playback state of A and B? -> A should stay paused and B start playing When A is paused (in this case, by a manual pause), B is playing (maybe by a manual start), you manually start A -> what is the expected playback state of A and B? -> A should play paused and B is paused

eneim commented 3 years ago

@loicsiret Thanks, let me consider a snapshot so that you can try. My current manual playback mechanism is having some issues, hope I can handle them as well.

For the last answer, just to clarify, it is

-> A should play ~paused~ and B is paused

right?

loicsiret commented 3 years ago

oops you are totally right !

eneim commented 3 years ago

@loicsiret I push an update that you can try with the snapshot build:

Follow this step, but add the following line below jcenter():

maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }

Add the following to your app dependency

  def releaseVersionName = "1.3.0.2011008-SNAPSHOT"
  implementation "im.ene.kohii:kohii-core:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-exoplayer:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-androidx:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-experiments:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-ads:${releaseVersionName}"

You can also try the sample app that comes with the source code of this library.

hantrungkien commented 3 years ago

@loicsiret I push an update that you can try with the snapshot build:

Follow this step, but add the following line below jcenter():

maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }

Add the following to your app dependency

  def releaseVersionName = "1.3.0.2011008-SNAPSHOT"
  implementation "im.ene.kohii:kohii-core:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-exoplayer:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-androidx:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-experiments:${releaseVersionName}"
  implementation "im.ene.kohii:kohii-ads:${releaseVersionName}"

You can also try the sample app that comes with the source code of this library.

@eneim Hi bro, are you planning to merge this version yet?

rahul-sysquare commented 1 day ago

@eneim Is this manual playback mechanism code merged into main branch?