Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

play / pause when clicked #105

Open
iregados opened this issue Sep 18, 2020 · 9 comments
Open

play / pause when clicked #105

iregados opened this issue Sep 18, 2020 · 9 comments

Comments

@iregados
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

eneim commented Sep 19, 2020

@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
Copy link
Author

iregados commented Sep 19, 2020 via email

@eneim
Copy link
Owner

eneim commented Sep 19, 2020

@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
Copy link

@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
Copy link

@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
Copy link
Owner

eneim commented Jul 20, 2024

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 {}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants