From db1eb144369013f50a277516e6886b1285935807 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 23 Sep 2022 13:59:00 +0200 Subject: [PATCH] catch player errors --- .../libretube/ui/fragments/PlayerFragment.kt | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index d25c8a525..dc3f23ac6 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -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