保存外层位置以在关闭视图后继续播放(OnDestroy())
正如我已经了解的那样,当我在观看视频时退出播放器视图或不按暂停按钮,onpause()和onStop()方法开始发挥作用,如果我们按下返回按钮,则ondestroy()方法开始发挥作用,并且摧毁视图。
以下代码达到的是,当我离开播放器的视图或不暂停按钮时,例如以提高屏幕的亮度,我返回视频时,剩下的范围继续进行。那对我有好处。但是,当我按下后面按钮并再次播放视频时,它是从一开始就开始的。
我可以在onCreate()方法中控制它,但是找不到正确的键。
有什么想法吗?谢谢
private lateinit var binding: ActivityExoPlayerPlayPeliBinding
private var player: ExoPlayer? = null
private var playWhenReady = true
private var currentItem = 0
private var playbackPosition: Long = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityExoPlayerPlayPeliBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onStart() {
super.onStart()
initializePlayer()
}
override fun onPause() {
super.onPause()
savePos()
}
override fun onStop() {
super.onStop()
savePos()
}
override fun onResume() {
super.onResume()
hideSystemUi()
continuePlayer(savePos())
}
override fun onDestroy() {
super.onDestroy()
savePos()
}
private fun savePos(): Long {
if (player != null){
playbackPosition = player!!.currentPosition
player!!.removeListener(this)
player!!.release()
player = null
}
return playbackPosition
}
private fun continuePlayer(playPosition: Long) {
player = ExoPlayer.Builder(applicationContext).build()
player!!.addListener(this)
binding.videoView.player = player
val url = intent.getStringExtra("url")
val mediaItem = item(url!!)
player!!.setMediaItem(mediaItem)
player!!.isCurrentMediaItemLive
playWhenReady
player!!.seekTo(currentItem, playPosition)
player!!.prepare()
player!!.play()
}
private fun initializePlayer() {
player = ExoPlayer.Builder(applicationContext).build()
player!!.addListener(this)
binding.videoView.player = player
val url = intent.getStringExtra("url")
val mediaItem = item(url!!)
player!!.setMediaItem(mediaItem)
player!!.isCurrentMediaItemLive
playWhenReady
player!!.seekTo(currentItem, playbackPosition)
player!!.prepare()
player!!.play()
}
As I have understood, when I exit the player view while watching a video by pressing or not the pause button, the onPause() and onStop() methods come into play and if we pressing back button the onDestroy() method comes into play and destroy the view.
The following code achieves that when I leave the view of the player pressing or not to the pause button, for example to increase the brightness of the screen and I return to the video, the reproduction continues for where it was left. That's good for me. But when I press the back button and play the video again, it starts from the beginning.
I could control it in the onCreate() method, but I can't find the right key.
Any idea? Thank you
private lateinit var binding: ActivityExoPlayerPlayPeliBinding
private var player: ExoPlayer? = null
private var playWhenReady = true
private var currentItem = 0
private var playbackPosition: Long = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityExoPlayerPlayPeliBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onStart() {
super.onStart()
initializePlayer()
}
override fun onPause() {
super.onPause()
savePos()
}
override fun onStop() {
super.onStop()
savePos()
}
override fun onResume() {
super.onResume()
hideSystemUi()
continuePlayer(savePos())
}
override fun onDestroy() {
super.onDestroy()
savePos()
}
private fun savePos(): Long {
if (player != null){
playbackPosition = player!!.currentPosition
player!!.removeListener(this)
player!!.release()
player = null
}
return playbackPosition
}
private fun continuePlayer(playPosition: Long) {
player = ExoPlayer.Builder(applicationContext).build()
player!!.addListener(this)
binding.videoView.player = player
val url = intent.getStringExtra("url")
val mediaItem = item(url!!)
player!!.setMediaItem(mediaItem)
player!!.isCurrentMediaItemLive
playWhenReady
player!!.seekTo(currentItem, playPosition)
player!!.prepare()
player!!.play()
}
private fun initializePlayer() {
player = ExoPlayer.Builder(applicationContext).build()
player!!.addListener(this)
binding.videoView.player = player
val url = intent.getStringExtra("url")
val mediaItem = item(url!!)
player!!.setMediaItem(mediaItem)
player!!.isCurrentMediaItemLive
playWhenReady
player!!.seekTo(currentItem, playbackPosition)
player!!.prepare()
player!!.play()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在其他地方保存位置,然后在活动或碎片中检索它。最简单的方法是通过共享流程
you have to save the position elsewhere and then retrieve it in your Activity or Fragment. The easiest way to do this is through SharedPreferences