保存外层位置以在关闭视图后继续播放(OnDestroy())

发布于 2025-01-29 08:38:24 字数 2337 浏览 5 评论 0原文

正如我已经了解的那样,当我在观看视频时退出播放器视图或不按暂停按钮,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 技术交流群。

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

发布评论

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

评论(1

孤独陪着我 2025-02-05 08:38:24

您必须在其他地方保存位置,然后在活动或碎片中检索它。最简单的方法是通过共享流程

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

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