From 90c6e751bd5d5a49f55dc57cbc2205dc22ce6b90 Mon Sep 17 00:00:00 2001 From: jerry08 Date: Fri, 23 Dec 2022 17:09:02 -0400 Subject: [PATCH] Code refactor & fixed video player crashing sometimes --- AniStream/VideoActivity.cs | 60 ++++++++++++-------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/AniStream/VideoActivity.cs b/AniStream/VideoActivity.cs index 89e25f6..047c662 100644 --- a/AniStream/VideoActivity.cs +++ b/AniStream/VideoActivity.cs @@ -61,7 +61,7 @@ public class VideoActivity : AppCompatActivity, IPlayer.IListener, private readonly AnimeClient _client = new(WeebUtils.AnimeSite); private readonly PlayerSettings _playerSettings = new(); - + private Anime Anime = default!; private Episode Episode = default!; private Video Video = default!; @@ -97,7 +97,7 @@ public class VideoActivity : AppCompatActivity, IPlayer.IListener, private Handler Handler { get; set; } = new Handler(Looper.MainLooper!); - private bool IsPipEnabled { get; set; } = false; + private bool IsPipEnabled { get; set; } private Rational AspectRatio { get; set; } = new(16, 9); private bool PlayAfterEnteringPipMode { get; set; } = false; @@ -126,17 +126,14 @@ protected async override void OnCreate(Bundle? savedInstanceState) //Enable unhandled exceptions for testing AndroidEnvironment.UnhandledExceptionRaiser += (s, e) => { - }; - + AppDomain.CurrentDomain.UnhandledException += (s, e) => { - }; - + TaskScheduler.UnobservedTaskException += (s, e) => { - }; await _playerSettings.LoadAsync(); @@ -151,13 +148,13 @@ protected async override void OnCreate(Bundle? savedInstanceState) var episodeString = Intent.GetStringExtra("episode"); if (!string.IsNullOrEmpty(episodeString)) Episode = JsonConvert.DeserializeObject(episodeString)!; - + var bookmarkManager = new BookmarkManager("recently_watched"); var isBooked = await bookmarkManager.IsBookmarked(Anime); if (isBooked) bookmarkManager.RemoveBookmark(Anime); - + bookmarkManager.SaveBookmark(Anime, true); //NetworkStateReceiver = new NetworkStateReceiver(); @@ -172,7 +169,7 @@ protected async override void OnCreate(Bundle? savedInstanceState) exoQuality = FindViewById(Resource.Id.exo_quality)!; //progressBar = FindViewById(Resource.Id.buffer)!; progressBar = FindViewById(Resource.Id.exo_init_buffer)!; - + //errorText = FindViewById(Resource.Id.errorText)!; VideoInfo = FindViewById(Resource.Id.exo_video_info)!; VideoName = FindViewById(Resource.Id.exo_video_name)!; @@ -206,7 +203,7 @@ protected async override void OnCreate(Bundle? savedInstanceState) TimeStampText = FindViewById(Resource.Id.exo_time_stamp_text)!; var exoSpeed = FindViewById(Resource.Id.exo_playback_speed)!; var exoScreen = FindViewById(Resource.Id.exo_screen)!; - + var backButton = FindViewById(Resource.Id.exo_back)!; var lockButton = FindViewById(Resource.Id.exo_lock)!; @@ -225,7 +222,7 @@ protected async override void OnCreate(Bundle? savedInstanceState) if (Build.VERSION.SdkInt >= BuildVersionCodes.N) { IsPipEnabled = PackageManager!.HasSystemFeature(PackageManager.FeaturePictureInPicture); - + if (IsPipEnabled) { exoPip.Visibility = ViewStates.Visible; @@ -663,14 +660,14 @@ private async void LoadTimeStamps() SkippedTimeStamps.AddRange(timeStamps); var adGroups = new List(); - for (int i = 0; i < timeStamps.Count; i++) + for (var i = 0; i < timeStamps.Count; i++) { adGroups.Add((long)(timeStamps[i].Interval.StartTime * 1000)); adGroups.Add((long)(timeStamps[i].Interval.EndTime * 1000)); } var playedAdGroups = new List(); - for (int i = 0; i < timeStamps.Count; i++) + for (var i = 0; i < timeStamps.Count; i++) { playedAdGroups.Add(false); playedAdGroups.Add(false); @@ -718,7 +715,7 @@ private void UpdateTimeStamp() else { SkipTimeButton.Visibility = ViewStates.Gone; - ExoSkip.Visibility= ViewStates.Visible; + ExoSkip.Visibility = ViewStates.Visible; } Handler.PostDelayed(() => @@ -766,7 +763,7 @@ public async Task UpdateProgress() return; _playerSettings.WatchedEpisodes.TryGetValue(Episode.Link, - out WatchedEpisode? watchedEpisode); + out var watchedEpisode); watchedEpisode ??= new(); @@ -861,7 +858,7 @@ public async void PlayVideo(Video video) exoPlayer.PlayWhenReady = true; _playerSettings.WatchedEpisodes.TryGetValue(Episode.Link, - out WatchedEpisode? watchedEpisode); + out var watchedEpisode); if (watchedEpisode is not null) exoPlayer.SeekTo(watchedEpisode.WatchedDuration); @@ -875,12 +872,10 @@ await Task.Run(async () => public void OnMediaItemTransition(MediaItem? mediaItem, int reason) { - } public void OnAvailableCommandsChanged(Commands? availableCommands) { - } public void OnPlaybackStateChanged(int playbackState) @@ -895,17 +890,18 @@ public void OnPlaybackStateChanged(int playbackState) public void OnPlaybackSuppressionReasonChanged(int playbackSuppressionReason) { - } public void OnRepeatModeChanged(int repeatMode) { - } public void OnAudioSessionIdChanged(int audioSessionId) { + } + public void OnMediaMetadataChanged(MediaMetadata? mediaMetadata) + { } public void OnTracksChanged(Tracks? tracks) @@ -926,15 +922,11 @@ public void OnTracksChanged(Tracks? tracks) if (exoQuality.HasOnClickListeners) return; - exoQuality.Click += (s, e) => - { - ShowM3U8TrackSelector(); - }; + exoQuality.Click += (s, e) => ShowM3U8TrackSelector(); } public void OnTimelineChanged(Timeline? timeline, int reason) { - } public override void OnWindowFocusChanged(bool hasFocus) @@ -979,57 +971,46 @@ public void OnIsPlayingChanged(bool isPlaying) public void OnLoadingChanged(bool isLoading) { - } public void OnPlaybackParametersChanged(PlaybackParameters? playbackParameters) { - } public void OnPositionDiscontinuity(int reason) { - } public void OnSeekProcessed() { - } public void OnShuffleModeEnabledChanged(bool shuffleModeEnabled) { - } public void OnPlayerStateChanged(bool playWhenReady, int playbackState) { - } public void OnPlayWhenReadyChanged(bool playWhenReady, int reason) { - } public void OnEvents(IPlayer? player, Events? events) { - } public void OnSurfaceSizeChanged(int width, int height) { - } public void OnIsLoadingChanged(bool isLoading) { - } public void OnVideoSizeChanged(VideoSize? videoSize) { - } public void OnRenderedFirstFrame() @@ -1064,17 +1045,14 @@ public void OnPlayerErrorChanged(PlaybackException? error) public void OnDeviceVolumeChanged(int volume, bool muted) { - } public void NetworkAvailable() { - } public void NetworkUnavailable() { - } public string? GetTrackName(Format? format) @@ -1092,7 +1070,6 @@ public void NetworkUnavailable() public void OnTrackSelectionParametersChanged(TrackSelectionParameters? parameters) { - } #pragma warning disable CS0618 @@ -1114,7 +1091,6 @@ private void EnterPipMode() } catch (Exception e) { - } }