eneim / kohii

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

play / pause when clicked #105

Open iregados opened 3 years ago

iregados commented 3 years ago

In my fragment i've done

val kohii = Kohii[this]
val manager = kohii.register(this).addBucket(binding.recyclerView)

then I pass them to my adapter like

 val adapter = MainTimelineAdapter(timelineList, kohii, manager)

on my adapter i have done

        kohii.setUp(videoUrl!!) {
            tag = "${videoUrl}+${position}"
            threshold = .8F
        }.bind(holder.binding.videoExoplayer){ playback ->
            val playable = playback.playable
            val myController = MyController(playable, manager)
            holder.binding.videoExoplayer.setOnClickListener(myController)
            KParameter.Kind.INSTANCE
        }

where myController looks like

class MyController(
    val playable: Playable?,
    val manager: Manager
) : View.OnClickListener, Playback.Controller {

    override fun kohiiCanStart() = true

    override fun kohiiCanPause() = true

    override fun onClick(v: View?) {
        if (playable != null) {
            val playing =playable.isPlaying()
            if (playable.isPlaying())
                manager.pause(playable)
            else
                manager.play(playable)
        }
    }
}

I want it to play and pause the video when the video is clicked, but it is actually doing nothing, even with manager and playable beeing not null...

I really dont know what I am doing wrong in here

iregados commented 3 years ago

achieved it when i setted the controller also, like

    kohii.setUp(videoUrl!!) {
            tag = "${videoUrl}+${position}"
            threshold = .8F
            controller = object : Playback.Controller {
                override fun kohiiCanStart(): Boolean = true
                override fun kohiiCanPause(): Boolean = true
            }
        }.bind(holder.binding.videoExoplayer){ playback ->
            val playable = playback.playable
            val myController = MyController(playable, manager)
            holder.binding.videoExoplayer.setOnClickListener(myController)
            KParameter.Kind.INSTANCE
        }

now I got another problem, when i pause the video there is no image remaining on screen, it's totally gone, is it possible to let the paused video image on screen when paused?

another doubt, is it possible to set something like "center_crop" to the video?

sometimes I got images on the adapter and sometimes videos, the images are center crop, if the video is not center cropped it looks kinda strange when compared to the images...

iregados commented 3 years ago

ok i believe that I am almost there, now i have done

        <FrameLayout
                android:id="@+id/playerViewContainer"
                app:layout_constraintTop_toTopOf="parent"
                android:visibility='@{(tweet.retweetedStatus == null ? tweet : tweet.retweetedStatus).mediaEntities.size() > 0 ? ((tweet.retweetedStatus == null ? tweet : tweet.retweetedStatus).mediaEntities[0].type.equals("photo") ) ? View.GONE : View.VISIBLE : View.GONE}'
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/black">
                <com.google.android.exoplayer2.ui.PlayerView
                    android:id="@+id/playerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:resize_mode="fixed_width"/>
            </FrameLayout>

on my layout file as you recommend in one of your tutorials

I am setting the playerView height with a globalLayoutListener because every video can get a different aspect ratio..

my rebinder looks like:

    kohii.setUp(videoUrl!!) {
        tag = "${videoUrl}+${position}"
        threshold = .8F
        repeatMode = Common.REPEAT_MODE_ALL
        controller = object : Playback.Controller {
            override fun kohiiCanStart(): Boolean = false
            override fun kohiiCanPause(): Boolean = true
            override fun setupRenderer(playback: Playback, renderer: Any?) {
                val playable = playback.playable ?: return
                playback.manager.play(playable)
                playerViewContainer.setOnClickListener {
                    if (playable.isPlaying()) {
                        playback.manager.pause(playable)
                    } else {
                        playback.manager.play(playable)
                    }
                }
            }

        }
    }.bind(playerView)

The video is there, if i pause it the image remains there, the dimension is ok, but while scrolling something wired happens, previous video's sound start randomly playing even if there is no playerView on screen, its like its not released i believe..

should I do anything to release it on my adapter?

iregados commented 3 years ago

done it...had to "clear" the PlayerViews as i scroll...something like Glide does with .clear().....if there is a video on my holder i setup the right url...if there is no video on my holder i setup an empty url and bind it to the gone PlayerView just to avoid recycling mistakes...

is it right?

Man such a powerfull library and yet really simple to work with....you are a monster

eneim commented 3 years ago

@iregados I saw you have solved the play/pause on click issue. So the remaining issue is:

if i pause it the image remains there, the dimension is ok, but while scrolling something wired happens, previous video's sound start randomly playing even if there is no playerView on screen, its like its not released i believe

I believe. I will try to reproduce and see.

iregados commented 3 years ago

This is actually solved by setting an empty url and binding it to the PlayerViews where there is no video to display.

The problem is not with your library but with how Android recycle things... All I need to do is "clear" those PlayerView without videos to show.

You could create a clear function.. Something like Glide does

Em sáb, 19 de set de 2020 08:35, Nam Nguyen Hoai notifications@github.com escreveu:

@iregados https://github.com/iregados I saw you have solved the play/pause on click issue. So the remaining issue is:

if i pause it the image remains there, the dimension is ok, but while scrolling something wired happens, previous video's sound start randomly playing even if there is no playerView on screen, its like its not released i believe

I believe. I will try to reproduce and see.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eneim/kohii/issues/105#issuecomment-695202361, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAS6EDSFYKLCOME2QZTT3ZDSGSJPJANCNFSM4RRL4ILA .

eneim commented 3 years ago

@iregados Thanks for the kind words. I also acknowledge that the RecyclerView may behave weird. Something a View not in the viewport is attached, etc. I actually try to address that by calculating the View's coordinator and some other metric to know if it is visible or not. Though it maybe not yet enough. I will look into both, one is a kill-switch that client can use in such cases, another thing is to see what can the library do automatically as well.

crema-git commented 4 months ago

@iregados Could you please share the code snaps that worked for you? I am using the following code base but didn't work for me

 kohii.setUp(media.getMediaUrl()) {
                    threshold = 0.5F
                    preload = true
                    repeatMode = Player.REPEAT_MODE_ONE
                    controller = object : Playback.Controller {
                               override fun kohiiCanStart(): Boolean = true
                                override fun kohiiCanPause(): Boolean = true
                                override fun setupRenderer(playback: Playback, renderer: Any?) {
                                    binding.playerContainer.setOnClickListener {
                                           val playable = playback.playable ?: return@setOnClickListener
                                            if (playable.isPlaying()) {
                                                  playback.manager.pause(playable)
                                                } else {
                                                  playback.manager.play(playable)
                                                }
                                          }
                                    }

                              }
                }.bind(binding.container);

but in case I add the tag it will start working but hide the videoview on pause

rahul-sysquare commented 4 months ago

@iregados Could you please share the code snaps that worked for you? I am using the following code base but didn't work for me

 kohii.setUp(media.getMediaUrl()) {
                    threshold = 0.5F
                    preload = true
                    repeatMode = Player.REPEAT_MODE_ONE
                    controller = object : Playback.Controller {
                               override fun kohiiCanStart(): Boolean = true
                                override fun kohiiCanPause(): Boolean = true
                                override fun setupRenderer(playback: Playback, renderer: Any?) {
                                    binding.playerContainer.setOnClickListener {
                                           val playable = playback.playable ?: return@setOnClickListener
                                            if (playable.isPlaying()) {
                                                  playback.manager.pause(playable)
                                                } else {
                                                  playback.manager.play(playable)
                                                }
                                          }
                                    }

                              }
                }.bind(binding.container);

but in case I add the tag it will start working but hide the videoview on pause

@eneim @iregados @sdex does anyone have any solutions for this?

eneim commented 1 month ago

but in case I add the tag it will start working but hide the videoview on pause

You can use PlayerView rather than a non-PlayerView. The PlayerView will not be removed for recycling.

For debugging, I suggest using another View in the same ViewHolder to receive the click event (replace binding.playerContainer.setOnClickListener by something else like binding.customButton.setOnClickListener {}).