活动恢复后,Kotlin VideoView将无法播放

时间:2019-05-26 16:45:58

标签: android kotlin

我正在SplashActivity中加载ViewView,目前这是我的配置。

class SplashActivity : AppCompatActivity(){
lateinit var layouts: IntArray
lateinit var videoView: VideoView
lateinit var mediaPlayers: MediaPlayer
lateinit var uri: Uri
var playbackPosition = 0

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_splash)
        videoView = findViewById(R.id.mVideoView)
        uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.bg_video)
        videoView.setVideoURI(uri)
        videoView.start()
        videoView.setOnPreparedListener { mediaPlayer ->
            mediaPlayer.isLooping = true
            mediaPlayers = mediaPlayer
        }
    }

override fun onStart() {
        super.onStart()
        videoView.setVideoURI(uri)
    }

    override fun onPause() {
        super.onPause()
        videoView.pause()
        playbackPosition = videoView.currentPosition
    }

    override fun onStop() {
        videoView.stopPlayback()
        super.onStop()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        println("position: ${videoView.currentPosition}")
        outState.putInt("Position", videoView.currentPosition)
        videoView.pause()
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        if (savedInstanceState != null) playbackPosition = savedInstanceState.getInt("Position")
        videoView.seekTo(playbackPosition)
        videoView.start()
    }
}

但是,当活动继续进行时,该位置始终为0。 有人知道如何解决这个问题吗?。

存在一些相关问题,例如:Video does not resume in VideoViewVideoview Pausing and resuming,但是两者的解决方案始终会导致黑屏。

0 个答案:

没有答案