exoplayer 发布时音频继续播放
我在对话框中使用 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()
之前尝试过 .stop
、clearVideoSurface()
、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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好像您调用了
initializePlayer()
两次。导致两个 Exoplayer 实例正在播放;您只能释放simpleExoPlayer
变量所引用的那个。Seems like you called
initializePlayer()
twice. resulting in two Exoplayer instances playing; you're only able to release the one thesimpleExoPlayer
variable holds a reference to.