static void MoveTo(PlaylistEntry entry) { if (playbackList != null) { SaveCurrentPosition(); var index = playbackList.Items.IndexOf(entry.AssociatedPlaybackItem); if (index >= 0 && playbackList.CurrentItemIndex != index && index < playbackList.Items.Count) { Debug.WriteLine("Moving to:" + index); try { trackedPosition = 0; SyncState(MediaPlaybackState.Buffering); playbackList.MoveTo((uint)index); } catch (Exception ex) { CoreTools.ShowDebugToast(ex.Message, "MoveTo"); } } } }
public static async Task RunOnDispatcherAsync(Action action) { if (CoreTools.GlobalDispatcher == null || CoreTools.GlobalDispatcher.HasThreadAccess) { try { action(); } catch (Exception ex) { CoreTools.ShowDebugToast(ex.Message, "RunOnDispatcherAsync"); } return; } await CoreTools.GlobalDispatcher.AwaitableRunAsync(() => { try { action(); } catch (Exception ex) { CoreTools.ShowDebugToast(ex.Message, "RunOnDispatcherAsync"); } }); }
static async Task CreatePlaylistAsync() { try { if (Playlist.CurrentPlaylist.Entries.Count == 0) { return; } if (playbackList != null) { playbackList.CurrentItemChanged -= PlaybackList_CurrentItemChanged; } playbackList = new MediaPlaybackList(); playbackList.CurrentItemChanged += PlaybackList_CurrentItemChanged; var currentList = Playlist.CurrentPlaylist.Entries.ToList(); foreach (var entry in currentList) { await AddSourceToPlaylistAsync(entry); } // Set playlist if (playbackList.Items.Count == 0) { return; } var currentIndex = Math.Min(Playlist.CurrentPlaylist.Entries.Count - 1, Math.Max(0, Playlist.CurrentPlaylist.CurrentIndex)); playbackList.StartingItem = playbackList.Items[currentIndex]; Position = Playlist.CurrentPlaylist.Entries[currentIndex].Position; var shouldAutoPlay = ForcePlayNext || AppSettings.Instance.AutoPlay; Player.AutoPlay = shouldAutoPlay; Player.Source = playbackList; if (shouldAutoPlay) { Play(); } ForcePlayNext = false; } catch (Exception ex) { App.TrackException(ex); CoreTools.ShowDebugToast(ex.Message, "CreatePlaylistAsync"); } }
public static void Play() { try { Debug.WriteLine("Play"); Player.Play(); lastOrderReceived = LastMediaOrder.Play; ForcePlayNext = false; } catch (Exception ex) { CoreTools.ShowDebugToast(ex.Message, "Play"); } }
private static void Player_MediaFailed(MediaPlayer sender, MediaPlayerFailedEventArgs args) { CoreTools.ShowDebugToast(args.ErrorMessage, "Player_MediaFailed"); App.TrackEvent(args.ErrorMessage); App.TrackException(args.ExtendedErrorCode); SaveCurrentPosition(); // If network related... if (!NetworkAvailabilityCheckInProgress && !NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable) { NetworkAvailabilityCheckInProgress = true; NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged; } }
public static void Play() { try { Debug.WriteLine("Play"); if (Player.PlaybackSession.PlaybackState == MediaPlaybackState.None || Player.PlaybackSession.PlaybackState == MediaPlaybackState.Paused) { Player.Play(); } lastOrderReceived = LastMediaOrder.Play; ForcePlayNext = false; } catch (Exception ex) { CoreTools.ShowDebugToast(ex.Message, "Play"); } }
public static async void Pause(bool publish) { try { Debug.WriteLine("Pause"); Player.Pause(); lastOrderReceived = LastMediaOrder.Pause; await Playlist.CurrentPlaylist.SaveAsync(); if (publish) { await Playlist.CurrentPlaylist.PublishAsync(); } } catch (Exception ex) { CoreTools.ShowDebugToast(ex.Message, "Pause"); } }
public static void SaveCurrentPosition(PlaylistEntry currentEntry = null, double?currentPosition = null, bool force = false) { try { if (!currentPosition.HasValue) { currentPosition = Position; } if (currentEntry == null) { currentEntry = CurrentEntry; } if (!force) { if (Playlist.CurrentPlaylist == null || playbackList?.CurrentItem == null || previousState != MediaPlaybackState.Playing) { return; } } if (currentEntry != null && currentEntry.Duration > 1 && currentPosition > 1) { // Debug.WriteLine("SaveCurrentPosition to " + currentEntry.Episode.Title + "(" + currentPosition + ")"); currentEntry.Position = currentPosition.Value; if ((currentEntry.Duration >= currentPosition) && (currentPosition / currentEntry.Duration) > 0.95 && currentEntry.Episode != null) { currentEntry.Episode.IsPlayed = true; } } } catch (Exception ex) { App.TrackException(ex); CoreTools.ShowDebugToast(ex.Message, "SaveCurrentPosition"); } }
private static async void PlaybackList_CurrentItemChanged(MediaPlaybackList sender, CurrentMediaPlaybackItemChangedEventArgs args) { try { if (args.NewItem == null) { return; } var currentItem = sender.CurrentItem; if (currentItem == null) { return; } CheckPlaylistState(sender); syncedItem = playbackList.CurrentItem; RestorePosition(); if (CurrentEntry == null) { return; } Debug.WriteLine("Switched to episode#" + CurrentEntry.Episode.Title); if (currentItem.Source.Duration.HasValue) { CurrentEntry.Duration = currentItem.Source.Duration.Value.TotalSeconds; } else { CurrentEntry.Duration = 0; } if (LocalSettings.Instance.VideoPlayback && args.NewItem.VideoTracks.Count > 0) { videoPlayInProgess = true; RaiseVideoPlayerEngaged(); } else if (videoPlayInProgess) { videoPlayInProgess = false; RaiseVideoPlayerDisengaged(); } // Playback rate try { Player.PlaybackSession.PlaybackRate = LocalSettings.Instance.PlaySpeed / 100.0; } catch (Exception) { LocalSettings.Instance.PlaySpeed = 100; } if (!(Application.Current as App).IsInBackground && CurrentEntry != null && CurrentEntry.IsStreaming && NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable && NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection && !LocalSettings.Instance.StreamOnMetered) { await DispatchManager.RunOnDispatcherAsync(async() => { Pause(false); await App.MessageAsync(StringsHelper.StreamingIsDisabled); }); } SyncState(Player.PlaybackSession.PlaybackState); } catch (Exception ex) { App.TrackException(ex); CoreTools.ShowDebugToast(ex.Message, "PlaybackList_CurrentItemChanged"); } }