fix: 修复视频播放发热问题 - WebView暂停/销毁、亮度恢复
This commit is contained in:
@@ -80,6 +80,12 @@ class PlayerActivity : AppCompatActivity() {
|
||||
private var startVolume = 0
|
||||
private var maxVolume = 0
|
||||
private val indicatorHideHandler = Handler(Looper.getMainLooper())
|
||||
private var savedBrightness = 0f
|
||||
private val webViewClient = object : WebViewClient() {
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
showLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -104,6 +110,8 @@ class PlayerActivity : AppCompatActivity() {
|
||||
resumePosition = if (intent.hasExtra("resume_position")) intent.getLongExtra("resume_position", 0) else null
|
||||
|
||||
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
|
||||
savedBrightness = window.attributes.screenBrightness
|
||||
if (savedBrightness < 0) savedBrightness = -1f
|
||||
|
||||
btnClose.setOnClickListener { finish() }
|
||||
|
||||
@@ -324,6 +332,7 @@ class PlayerActivity : AppCompatActivity() {
|
||||
|
||||
private fun playWithExoPlayer(url: String) {
|
||||
playerWebView.visibility = View.GONE
|
||||
playerWebView.onPause()
|
||||
playerView.visibility = View.VISIBLE
|
||||
showLoading(false)
|
||||
|
||||
@@ -345,17 +354,13 @@ class PlayerActivity : AppCompatActivity() {
|
||||
stopPositionSaving()
|
||||
playerView.visibility = View.GONE
|
||||
playerWebView.visibility = View.VISIBLE
|
||||
playerWebView.onResume()
|
||||
showLoading(false)
|
||||
|
||||
playerWebView.settings.javaScriptEnabled = true
|
||||
playerWebView.settings.domStorageEnabled = true
|
||||
playerWebView.settings.mediaPlaybackRequiresUserGesture = false
|
||||
playerWebView.webViewClient = object : WebViewClient() {
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
super.onPageFinished(view, url)
|
||||
showLoading(false)
|
||||
}
|
||||
}
|
||||
playerWebView.webViewClient = webViewClient
|
||||
playerWebView.loadUrl(url)
|
||||
}
|
||||
|
||||
@@ -465,15 +470,29 @@ class PlayerActivity : AppCompatActivity() {
|
||||
super.onPause()
|
||||
saveCurrentPosition()
|
||||
stopPositionSaving()
|
||||
playerWebView.onPause()
|
||||
exoPlayer?.playWhenReady = false
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (playerWebView.visibility == View.VISIBLE) {
|
||||
playerWebView.onResume()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
saveCurrentPosition()
|
||||
stopPositionSaving()
|
||||
hideHandler.removeCallbacks(hideRunnable)
|
||||
indicatorHideHandler.removeCallbacksAndMessages(null)
|
||||
if (savedBrightness >= 0) {
|
||||
window.attributes.screenBrightness = savedBrightness
|
||||
}
|
||||
exoPlayer?.release()
|
||||
exoPlayer = null
|
||||
playerWebView.stopLoading()
|
||||
playerWebView.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user