Android Kotlin - 当应用进入后台时 ExoPlayer 继续播放
Android Kotlin - ExoPlayer keeps playing when app goes to background
到目前为止,我只发现了有关如何在应用程序进入后台时让 ExoPlayer 继续播放的问题。为什么我没有编码这个 bs 就是这种情况??
这是我目前所拥有的,它在 RecyclerView OnBingViewHolder 中:
val player = ExoPlayer.Builder(context).build()
val mediaItem: MediaItem = MediaItem.fromUri(fileUrl)
player.setMediaItem(mediaItem)
player.repeatMode = Player.REPEAT_MODE_ONE
holder.vidPlayer.player = player
player.prepare()
player.seekTo(100)
// player.play()
holder.vidPlayer.setTag(mpTag, player)
holder.vidPlayer.setTag(manuelPlayTag, false)
holder.vidPlayer.setTag(manuelPauseTag, false)
player.addListener(object : Player.Listener { // player listener
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
if (playWhenReady && playbackState == Player.STATE_READY) {
Log.d(tagg, "state: plays")
holder.vidPlayer.hideController()
} else if (playWhenReady) {
// might be idle (plays after prepare()),
// buffering (plays when data available)
// or ended (plays when seek away from end)
} else {
Log.d(tagg, "state: pause")
holder.vidPlayer.showController()
}
}
})
当应用进入后台时如何阻止播放?
当您的应用进入后台时,会调用活动 Fragment/Activity 的生命周期方法 onPause
(和 onStop
)。在 onPause
方法中,您可以 并停止视频播放器。
当应用程序进入后台时,您可以简单地 stop
ExoPlayer。
override fun onStop() {
super.onStop()
simpleExoPlayer.stop()
}
在 onStart
中再次 prepare()
ExoPlayer:
override fun onStart() {
super.onStart()
simpleExoPlayer.prepare()
}
In order to automatically play the media, you need to set playWhenReady = true
.
simpleExoPlayer.playWhenReady = true
通过设置playWhenReady = true
它会自动播放内容,我们不需要显式调用simpleExoPlayer.play()
。
到目前为止,我只发现了有关如何在应用程序进入后台时让 ExoPlayer 继续播放的问题。为什么我没有编码这个 bs 就是这种情况??
这是我目前所拥有的,它在 RecyclerView OnBingViewHolder 中:
val player = ExoPlayer.Builder(context).build()
val mediaItem: MediaItem = MediaItem.fromUri(fileUrl)
player.setMediaItem(mediaItem)
player.repeatMode = Player.REPEAT_MODE_ONE
holder.vidPlayer.player = player
player.prepare()
player.seekTo(100)
// player.play()
holder.vidPlayer.setTag(mpTag, player)
holder.vidPlayer.setTag(manuelPlayTag, false)
holder.vidPlayer.setTag(manuelPauseTag, false)
player.addListener(object : Player.Listener { // player listener
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
if (playWhenReady && playbackState == Player.STATE_READY) {
Log.d(tagg, "state: plays")
holder.vidPlayer.hideController()
} else if (playWhenReady) {
// might be idle (plays after prepare()),
// buffering (plays when data available)
// or ended (plays when seek away from end)
} else {
Log.d(tagg, "state: pause")
holder.vidPlayer.showController()
}
}
})
当应用进入后台时如何阻止播放?
当您的应用进入后台时,会调用活动 Fragment/Activity 的生命周期方法 onPause
(和 onStop
)。在 onPause
方法中,您可以
当应用程序进入后台时,您可以简单地 stop
ExoPlayer。
override fun onStop() {
super.onStop()
simpleExoPlayer.stop()
}
在 onStart
中再次 prepare()
ExoPlayer:
override fun onStart() {
super.onStart()
simpleExoPlayer.prepare()
}
In order to automatically play the media, you need to set
playWhenReady = true
.
simpleExoPlayer.playWhenReady = true
通过设置playWhenReady = true
它会自动播放内容,我们不需要显式调用simpleExoPlayer.play()
。