eneim / kohii

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

Using kohii in two viewpager fragments #128

Open talha01sayed opened 3 years ago

talha01sayed commented 3 years ago

Hi there, Thanks for this great library. I am using kohii-sample-tiktok code but also I am using it in two tabs with view pager and FragmentPagerAdapter both tabs have TikTok like videos UI. But the problem I am facing is

java.lang.IllegalArgumentException: Failed requirement.

java.lang.IllegalArgumentException: Failed requirement. at kohii.v1.core.Binder.providePlayable(Binder.kt:85) at kohii.v1.core.Binder.bind(Binder.kt:65) at kohii.v1.core.Binder.bind$default(Binder.kt:62) at com.app.harmaika.infotainment.EpisodeAdapter.onBindViewHolder(EpisodeAdapter.kt:59) at com.app.harmaika.infotainment.EpisodeAdapter.onBindViewHolder(EpisodeAdapter.kt:19) at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7178) at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7258) at androidx.recyclerview.widget.NestedAdapterWrapper.onBindViewHolder(NestedAdapterWrapper.java:157) at androidx.recyclerview.widget.ConcatAdapterController.onBindViewHolder(ConcatAdapterController.java:368) at androidx.recyclerview.widget.ConcatAdapter.onBindViewHolder(ConcatAdapter.java:188) at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7178) at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7258) at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6125) at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6391) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6231)

I have added the code of kohii-sample-tiktok in both fragments but I am getting this issue. If I comment code from one fragment then other will work. Please help.

talha01sayed commented 3 years ago

This is my singleton class

object KohiiProvider {

private val capsule: Capsule<Kohii, Pair<Context, MediaSourceFactory>> =
        Capsule(creator = { (context, factory) ->
            createKohii(context, mediaSourceFactoryCreator = { factory })
        })

private fun get(context: Context, factory: MediaSourceFactory) = capsule.get(context to factory)

operator fun get(context: Context) = get(context, ProgressiveMediaSource.Factory {
    val dataSource = DefaultHttpDataSourceFactory("appPackageName",
            DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
            DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
            true)

    val dataSourceFactory = DefaultDataSourceFactory(
            context,
            null,
            dataSource
    )

    dataSourceFactory.createDataSource()
})

}

talha01sayed commented 3 years ago

This is my fragment code. both fragments have the same code

val kohii = KohiiProvider[requireContext()] kohii.register(this, memoryMode = MemoryMode.INFINITE, activeLifecycleState = Lifecycle.State.RESUMED) .addBucket(viewPager) passengersAdapter = EpisodeAdapter(this, categoryId, viewModel, kohii) viewPager.adapter = passengersAdapter.withLoadStateHeaderAndFooter( header = PassengersLoadStateAdapter { passengersAdapter.retry() }, footer = PassengersLoadStateAdapter { passengersAdapter.retry() } ) binding.viewPager.offscreenPageLimit = 2

talha01sayed commented 3 years ago

This is my both adapter code

kohii.setUp(videoFile) { tag = "video::$position" threshold = 0.5F preload = true repeatMode = Common.REPEAT_MODE_ONE artworkHintListener = object : Playback.ArtworkHintListener { override fun onArtworkHint( playback: Playback, shouldShow: Boolean, position: Long, state: Int ) { // holder.binding.thumbnail.isVisible = playback.playable?.isPlaying() == false } } controller = controller(kohiiCanStart = true, kohiiCanPause = true) { playback, _ -> val playable = playback.playable ?: return@controller holder.binding.container.setOnClickListener { if (playable.isPlaying()) playback.manager.pause(playable) else playback.manager.play(playable) } } }.bind(holder.binding.playerView)