exoplayer 发布时音频继续播放

发布于 2025-01-16 08:28:43 字数 2269 浏览 3 评论 0原文

我在对话框中使用 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
        }
    }
}

我在 .release() 之前尝试过 .stopclearVideoSurface()playerView.player = null。没有发挥作用。

I am using ExoPlayer in my dialog. I want the video to play automatically when dialog opens. When simpleExoPlayer.prepare() snippet is active I am able to do autoplay but when I close the dialog audio keeps playing. Before activating simpleExoPlayer.prepare() audio stops when I dismiss dialog. Is there another method to autoplay ExoPlayer or stop the audio when dialog dismiss?

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
        }
    }
}

I tried .stop, clearVideoSurface(), playerView.player = null before .release(). Didn't work it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2025-01-23 08:28:43

好像您调用了 initializePlayer() 两次。导致两个 Exoplayer 实例正在播放;您只能释放 simpleExoPlayer 变量所引用的那个。

Seems like you called initializePlayer() twice. resulting in two Exoplayer instances playing; you're only able to release the one the simpleExoPlayer variable holds a reference to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文