Kohii recipes

This recipe shows you how to observe the event when a Playback connected to a Playable is changed to another Playback

This recipe requires Kohii 1.1.0.* and above

API

playable observer - Kohii

Observe - Kohii

Sample code & Explanation

// Code below is executed from Fragment.onViewCreated()
val manager = Kohii[this].register(this@Fragment).addBucket(bucketView)
manager.observe(playableTag) { tag, previousPlayback, nextPlayback ->
	// TODO: do something with the informations
}

First, as a common setup of the library, you need to have a Kohii instance and register the Fragment or Activity as the Manager.

Kohii[this].register(this).addBucket(listView)

This line also returns a value of the Manager, and normally you can ignore that value. But in this case, the API we want to use is a method of the Manager, so we will get that value and call the method observe on that value.

The method observe accepts 2 parameters: the first parameter is the tag of a Video you want to observe, the second parameter is the callback. Every time a Playable changes its Playback, you will be notified with the value of the Playback before the change, and the Playback after the change. Both are nullable, as the Playback of a Playable can be null when it is released.

Use cases

A possible use case for this method is when you build a Video list that allows user to click a Video to open a full-screen player. When user clicks a Video, its Playable will be switch from one container to another container and therefore its Playback will be changed as well. Using the method in this recipe, you can observe that event to update the Video list before and after the switch.

Here is a similar example:

eneim/kohii