exoplayer 发布时音频继续播放

问题描述 投票:0回答:1

我在对话中使用 ExoPlayer。我希望视频在对话框打开时自动播放。当

simpleExoPlayer.prepare()
片段处于活动状态时,我可以进行自动播放,但是当我关闭对话框时,音频会继续播放。在激活
simpleExoPlayer.prepare()
之前,当我关闭对话框时音频停止。是否有另一种方法可以自动播放 ExoPlayer 或在对话框关闭时停止音频?

class VideoViewDialog (context: Context) : BaseDialog<LayoutDialogVideoViewBinding>(context) {
    private var videoUrl : String = ""
    private lateinit var simpleExoPlayer: ExoPlayer

    override fun populateUi() {
        setCanceledOnTouchOutside(true)
        mBinding?.apply {
            initializePlayer()
        }
    }

    private fun initializePlayer() {
        val mediaDataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(context)

        val mediaSource = ProgressiveMediaSource.Factory(mediaDataSourceFactory).createMediaSource(
            MediaItem.fromUri(videoUrl))

        val mediaSourceFactory: MediaSource.Factory = DefaultMediaSourceFactory(mediaDataSourceFactory)

        simpleExoPlayer = ExoPlayer.Builder(context)
            .setMediaSourceFactory(mediaSourceFactory)
            .build()

        simpleExoPlayer.addMediaSource(mediaSource)

        simpleExoPlayer.playWhenReady = true
        simpleExoPlayer.prepare()

        mBinding?.apply {
            playerView.player = simpleExoPlayer
            playerView.requestFocus()
        }
        simpleExoPlayer.play()
    }

    private fun releasePlayer() {
        simpleExoPlayer.release()
    }

    public override fun onStart() {
        super.onStart()

        if (Util.SDK_INT > 23) initializePlayer()
    }

    public override fun onStop() {
        super.onStop()

        if (Util.SDK_INT > 23) releasePlayer()
    }

    override fun getLayoutRes(): Int {
        return R.layout.layout_dialog_video_view
    }

    companion object{
        fun newInstance(
            context: Context,
            videoUrl : String,
        ) : VideoViewDialog {
            val dialog = VideoViewDialog(context)
            dialog.also {
                it.videoUrl = videoUrl
            }
            return dialog
        }
    }
}

我在

.stop
之前尝试了
clearVideoSurface()
playerView.player = null
.release()
。没用。

android kotlin exoplayer exoplayer2.x
1个回答
2
投票

好像你打了两次

initializePlayer()
。导致播放两个 Exoplayer 实例;你只能释放
simpleExoPlayer
变量持有的引用。

© www.soinside.com 2019 - 2024. All rights reserved.