Skip to content

Commit

Permalink
Code refactor & fixed video player crashing sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Dec 23, 2022
1 parent 74c4d57 commit 90c6e75
Showing 1 changed file with 18 additions and 42 deletions.
60 changes: 18 additions & 42 deletions AniStream/VideoActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -151,13 +148,13 @@ protected async override void OnCreate(Bundle? savedInstanceState)
var episodeString = Intent.GetStringExtra("episode");
if (!string.IsNullOrEmpty(episodeString))
Episode = JsonConvert.DeserializeObject<Episode>(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();
Expand All @@ -172,7 +169,7 @@ protected async override void OnCreate(Bundle? savedInstanceState)
exoQuality = FindViewById<ImageButton>(Resource.Id.exo_quality)!;
//progressBar = FindViewById<ProgressBar>(Resource.Id.buffer)!;
progressBar = FindViewById<ProgressBar>(Resource.Id.exo_init_buffer)!;

//errorText = FindViewById<TextView>(Resource.Id.errorText)!;
VideoInfo = FindViewById<TextView>(Resource.Id.exo_video_info)!;
VideoName = FindViewById<TextView>(Resource.Id.exo_video_name)!;
Expand Down Expand Up @@ -206,7 +203,7 @@ protected async override void OnCreate(Bundle? savedInstanceState)
TimeStampText = FindViewById<TextView>(Resource.Id.exo_time_stamp_text)!;
var exoSpeed = FindViewById<ImageButton>(Resource.Id.exo_playback_speed)!;
var exoScreen = FindViewById<ImageButton>(Resource.Id.exo_screen)!;

var backButton = FindViewById<ImageButton>(Resource.Id.exo_back)!;
var lockButton = FindViewById<ImageButton>(Resource.Id.exo_lock)!;

Expand All @@ -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;
Expand Down Expand Up @@ -663,14 +660,14 @@ private async void LoadTimeStamps()
SkippedTimeStamps.AddRange(timeStamps);

var adGroups = new List<long>();
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<bool>();
for (int i = 0; i < timeStamps.Count; i++)
for (var i = 0; i < timeStamps.Count; i++)
{
playedAdGroups.Add(false);
playedAdGroups.Add(false);
Expand Down Expand Up @@ -718,7 +715,7 @@ private void UpdateTimeStamp()
else
{
SkipTimeButton.Visibility = ViewStates.Gone;
ExoSkip.Visibility= ViewStates.Visible;
ExoSkip.Visibility = ViewStates.Visible;
}

Handler.PostDelayed(() =>
Expand Down Expand Up @@ -766,7 +763,7 @@ public async Task UpdateProgress()
return;

_playerSettings.WatchedEpisodes.TryGetValue(Episode.Link,
out WatchedEpisode? watchedEpisode);
out var watchedEpisode);

watchedEpisode ??= new();

Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -1092,7 +1070,6 @@ public void NetworkUnavailable()

public void OnTrackSelectionParametersChanged(TrackSelectionParameters? parameters)
{

}

#pragma warning disable CS0618
Expand All @@ -1114,7 +1091,6 @@ private void EnterPipMode()
}
catch (Exception e)
{

}
}

Expand Down

0 comments on commit 90c6e75

Please sign in to comment.