catch player errors

This commit is contained in:
Bnyro 2022-09-23 13:59:00 +02:00
parent e888b8a061
commit db1eb14436

View File

@ -78,6 +78,7 @@ import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.MediaItem.SubtitleConfiguration
import com.google.android.exoplayer2.MediaItem.fromUri
import com.google.android.exoplayer2.PlaybackException
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ext.cronet.CronetDataSource
@ -971,22 +972,6 @@ class PlayerFragment : BaseFragment() {
}
}
override fun onVideoSizeChanged(
videoSize: VideoSize
) {
// Set new width/height of view
// height or width must be cast to float as int/int will give 0
// Redraw the player container with the new layout height
val params = binding.player.layoutParams
params.height = videoSize.height / videoSize.width * params.width
binding.player.layoutParams = params
binding.player.requestLayout()
(binding.mainContainer.layoutParams as ConstraintLayout.LayoutParams).apply {
matchConstraintPercentHeight = (videoSize.height / videoSize.width).toFloat()
}
}
override fun onPlaybackStateChanged(playbackState: Int) {
exoPlayerView.keepScreenOn = !(
playbackState == Player.STATE_IDLE ||
@ -1032,6 +1017,18 @@ class PlayerFragment : BaseFragment() {
}
super.onPlaybackStateChanged(playbackState)
}
/**
* Catch player errors to prevent the app from stopping
*/
override fun onPlayerError(error: PlaybackException) {
Toast.makeText(
context,
error.localizedMessage,
Toast.LENGTH_LONG
).show()
super.onPlayerError(error)
}
})
// check if livestream