Android Studio (Kotlin) - VideoView 切换应用程序后变黑
Android Studio (Kotlin) - VideoView Goes Black After Switching Apps
我有一个非常简单的视频在我的主 activity 的底部三分之一处循环播放。当应用程序最小化然后我回来时,视频将停止播放,而是显示黑屏,而且似乎没有办法让它再次播放。 (我不想要视频控件。)
这是我的 MainActivity.kt:
import android.app.ActionBar
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import android.graphics.drawable.ColorDrawable
class MainActivity : AppCompatActivity() {
// declaring a null variable for VideoView
var simpleVideoView: VideoView? = null
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.hide()
// assigning id of VideoView from
// activity_main.xml layout file
simpleVideoView = findViewById<View>(R.id.videoView) as VideoView
// set the absolute path of the video file which is going to be played
simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
+ packageName + "/" + R.raw.gp))
simpleVideoView!!.requestFocus()
// starting the video
simpleVideoView!!.start()
simpleVideoView!!.setOnCompletionListener { simpleVideoView!!.start() }
}
}
最简单的解决方案是:
class 主活动:AppCompatActivity() {
var simpleVideoView: VideoView? = null
var currentPosition = 0
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.hide()
simpleVideoView = findViewById<View>(R.id.videoView) as VideoView
simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
+ packageName + "/" + R.raw.gp))
simpleVideoView!!.setOnCompletionListener { simpleVideoView!!.start() }
if (savedInstanceState != null) {
var position = savedInstanceState.getInt("position")
simpleVideoView!!.seekTo(position)
}
}
override fun onStart() {
super.onStart()
if (currentPosition != 0)
simpleVideoView!!.seekTo(currentPosition)
simpleVideoView!!.start()
}
override fun onResume() {
super.onResume()
if (!simpleVideoView!!.isPlaying) {
if (currentPosition != 0)
simpleVideoView!!.seekTo(currentPosition)
simpleVideoView!!.start()
}
}
override fun onPause() {
super.onPause()
simpleVideoView!!.pause()
currentPosition = simpleVideoView!!.currentPosition
}
override fun onStop() {
simpleVideoView!!.pause()
super.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt("position", currentPosition)
super.onSaveInstanceState(outState)
}
}
这会在旋转设备时离开的同一点重新开始播放视频。您可能想查看 android 中的 exoplayer,而不是 VideoView。
我有一个非常简单的视频在我的主 activity 的底部三分之一处循环播放。当应用程序最小化然后我回来时,视频将停止播放,而是显示黑屏,而且似乎没有办法让它再次播放。 (我不想要视频控件。)
这是我的 MainActivity.kt:
import android.app.ActionBar
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import android.graphics.drawable.ColorDrawable
class MainActivity : AppCompatActivity() {
// declaring a null variable for VideoView
var simpleVideoView: VideoView? = null
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.hide()
// assigning id of VideoView from
// activity_main.xml layout file
simpleVideoView = findViewById<View>(R.id.videoView) as VideoView
// set the absolute path of the video file which is going to be played
simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
+ packageName + "/" + R.raw.gp))
simpleVideoView!!.requestFocus()
// starting the video
simpleVideoView!!.start()
simpleVideoView!!.setOnCompletionListener { simpleVideoView!!.start() }
}
}
最简单的解决方案是:
class 主活动:AppCompatActivity() {
var simpleVideoView: VideoView? = null
var currentPosition = 0
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.hide()
simpleVideoView = findViewById<View>(R.id.videoView) as VideoView
simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
+ packageName + "/" + R.raw.gp))
simpleVideoView!!.setOnCompletionListener { simpleVideoView!!.start() }
if (savedInstanceState != null) {
var position = savedInstanceState.getInt("position")
simpleVideoView!!.seekTo(position)
}
}
override fun onStart() {
super.onStart()
if (currentPosition != 0)
simpleVideoView!!.seekTo(currentPosition)
simpleVideoView!!.start()
}
override fun onResume() {
super.onResume()
if (!simpleVideoView!!.isPlaying) {
if (currentPosition != 0)
simpleVideoView!!.seekTo(currentPosition)
simpleVideoView!!.start()
}
}
override fun onPause() {
super.onPause()
simpleVideoView!!.pause()
currentPosition = simpleVideoView!!.currentPosition
}
override fun onStop() {
simpleVideoView!!.pause()
super.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt("position", currentPosition)
super.onSaveInstanceState(outState)
}
}
这会在旋转设备时离开的同一点重新开始播放视频。您可能想查看 android 中的 exoplayer,而不是 VideoView。