与ViewPager一起使用时,如何在Exoplayer中播放或暂停视频?

时间:2019-07-19 10:56:03

标签: android android-viewpager exoplayer

我正在尝试在 ViewPager 中使用 Exoplayer 。仅当我通过单击用于显示视频的缩略图打开它时,它才会播放。但是当我通过滚动到达页面时,它将无法播放。

我尝试在 <内拨打 seekTo(0, 0) playWhenReady = true onScrolled() 中的em> onPageSelected() playWhenReady = false 使用Lambda的VPAdapter的strong>方法,但无效。

这是我用于viewPager的适配器。

class MediaVPAdapter(
    val context: Context,
    @LayoutRes val layoutRes: Int,
    val medias: List<MediaFiles> = listOf()
) : PagerAdapter() {

    private var player: SimpleExoPlayer? = null

    override fun isViewFromObject(view: View, o: Any): Boolean {
        return view == o
    }

    override fun getCount() = medias.size

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        return LayoutInflater.from(context).inflate(layoutRes, container, false).apply {
            if (medias.isNotEmpty()) {
                when (medias[position].mediaType) {
                    MediaType.PHOTO -> {
                        // Display the Image
                    }
                    MediaType.VIDEO -> {

                        ssImageView.makeInvisible(true)
                        exoPlayerView.makeVisible()
                        player = ExoPlayerFactory.newSimpleInstance(
                            DefaultRenderersFactory(context),
                            DefaultTrackSelector(),
                            DefaultLoadControl()
                        )
                        if (medias[position].uri != null)
                            initPlayer(this, medias[position].uri!!)

                    }
                    null -> {

                    }
                }
            }

            container.addView(this, 0)
        }
    }

    private fun initPlayer(view: View, uri: Uri) {
        Timber.i("initPlayer: uri: $uri")
        with(view) {
            player!!.let {
                exoPlayerView.player = it

                it.prepare(buildMediaSource(uri), true, false)

                it.playWhenReady = false
            }
        }
    }

    private fun buildMediaSource(uri: Uri) = ExtractorMediaSource.Factory(
        DefaultDataSourceFactory(context, Util.getUserAgent(context, context.getString(R.string.app_name)))
    )
        .setExtractorsFactory(DefaultExtractorsFactory())
        .createMediaSource(uri)


    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
        releasePlayer()
    }

    private fun releasePlayer() {
        if (player != null) {
            playWhenReady = false
            player!!.release()
            player = null
        }
    }
}

val mediaAdapter = MediaVPAdapter(
            this,
            R.layout.fragment_media,
            true,
            medias?.toList() ?: listOf()
        )
        vpMedia.adapter = mediaAdapter
        vpMedia.currentItem = position ?: 0
        vpMedia.offscreenPageLimit = 1

如果单击视频,则可以播放所选的视频,但是当我滚动到其他项目时,没有一个正在播放。当我尝试切换“播放/暂停”按钮时,只有该按钮在切换。

1 个答案:

答案 0 :(得分:0)

据我所知,您的代码缺少 setPlayWhenReady 侦听器。 您应通过在 initPlayer 方法中添加 it.setPlayWhenReady(playWhenReady) 来解决此问题。