I wrote a code for playing a video offline, but the problem is that when I rotate the screen, the video plays from the start again because currentPosition returns 0 It should be noted that I am using the Mac version of Android Studio
private fun initVideoView() {
val uri = Uri.parse("android.resource://$packageName/${R.raw.test_video2}")
val mediaController = MediaController(this)
try {
videoView.setMediaController(mediaController)
videoView.setVideoURI(uri)
videoView.start()
} catch (e: Exception) {
Log.e("Error", e.message ?: "Error")
}
videoView.setOnPreparedListener {
it.setOnVideoSizeChangedListener { _, _, _ ->
mediaController.setAnchorView(binding.videoView)
}
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("position", this.videoView.currentPosition)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
videoView.seekTo(savedInstanceState.getInt("position", 0))
}