public void removeFromPlayqueue(int itemId) { using (var db = new PlaylistDBContext()) { PlaylistItem itemToRemove = db.Playlist.FirstOrDefault(item => item.ItemId == itemId); if (itemToRemove != null) { PodcastEpisodeModel episode = null; using (var episodeDb = new PodcastSqlModel()) { episode = episodeDb.episodeForPlaylistItem(itemToRemove); if (episode != null) { if (episode.isListened()) { episode.markAsListened(episodeDb.settings().IsAutoDelete); } } else { Debug.WriteLine("Warning: Could not get episode for item id: " + itemToRemove.ItemId); } } db.Playlist.DeleteOnSubmit(itemToRemove); db.SubmitChanges(); App.mainViewModels.PlayQueue = new ObservableCollection <PlaylistItem>(); } } }
internal static PodcastEpisodeModel refreshEpisodeFromAudioAgent(PodcastEpisodeModel episode) { List <PlaylistItem> playlistItems = null; using (var playlistdb = new PlaylistDBContext()) { playlistItems = playlistdb.Playlist.ToList(); } PodcastEpisodeModel e = null; using (var db = new PodcastSqlModel()) { e = db.Episodes.FirstOrDefault(ep => ep.EpisodeId == episode.EpisodeId); bool deleteListened = false; if (playlistItems.Count > 0) { deleteListened = db.settings().IsAutoDelete; } foreach (PlaylistItem i in playlistItems) { if (i.EpisodeId != episode.EpisodeId) { continue; } Debug.WriteLine("Updating episode '" + e.EpisodeName + "' playpos to: " + i.SavedPlayPosTick); e.SavedPlayPos = i.SavedPlayPosTick; // Update play state to listened as appropriate. if (e.isListened()) { e.markAsListened(deleteListened); } db.SubmitChanges(); } } return(e); }
private void PlayStateChanged(object sender, EventArgs e) { EventHandler handlerStoppedPlaying = null; switch (BackgroundAudioPlayer.Instance.PlayerState) { case PlayState.Playing: PodcastEpisodeModel currentEpisode = updateCurrentlyPlayingEpisode(); if (currentEpisode == null) { Debug.WriteLine("Error: No playing episode in DB."); return; } if (CurrentlyPlayingEpisode == null) { CurrentlyPlayingEpisode = currentEpisode; } else if (currentEpisode.EpisodeId != CurrentlyPlayingEpisode.EpisodeId) { CurrentlyPlayingEpisode = currentEpisode; } if (CurrentlyPlayingEpisode.TotalLengthTicks == 0) { CurrentlyPlayingEpisode.StoreProperty <long>("TotalLengthTicks", BackgroundAudioPlayer.Instance.Track.Duration.Ticks); PodcastSubscriptionsManager.getInstance().podcastPlaystateChanged(CurrentlyPlayingEpisode.getSubscriptionModel()); } break; case PlayState.Paused: BackgroundAudioPlayer playerPaused = BackgroundAudioPlayer.Instance; if (CurrentlyPlayingEpisode != null) { CurrentlyPlayingEpisode = App.refreshEpisodeFromAudioAgent(CurrentlyPlayingEpisode); CurrentlyPlayingEpisode.StoreProperty <PodcastEpisodeModel.EpisodePlayStateEnum>("EpisodePlayState", PodcastEpisodeModel.EpisodePlayStateEnum.Paused); } else { Debug.WriteLine("SHOULD NOT HAPPEND! Cannot save episode state to paused!"); } break; case PlayState.Stopped: case PlayState.Shutdown: case PlayState.TrackEnded: if (CurrentlyPlayingEpisode == null) { // We didn't have a track playing. return; } // F**K YOU WINDOWS PHONE!! /** * This is so horrible that I can't find other words to describe how bad the BackgroundAudioPlayer * is in Windows Phone! * * First of all the events are fired totally differently between Windows Phone 7 and Windows Phone 8. * Secondly the events related to when tracks end arae totally wrong and horrible! Let me explain: * - We get here the PlayState.Stopped event when the track ends. * - We get the event here BEFORE the AudioAgent gets any events. * - AudioAgent correctly gets the PlayState.TrackEnded event, but it gets it after we have received the * event here. * - We NEVER get the the PlayState.TrackEnded event here. * - Which is not the case for Windows Phone 7, because it first fires PlayState.TrackEnded. * * So this code here is a horrible kludge that just guesses that Windows Phone means "track did end" * when the state is stopped and the play position is still 0. * * Johan, when you return from the future to this piece of code, don't even try to change it or "fix it". **/ long playpos = 0; if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.TrackEnded) { playpos = CurrentlyPlayingEpisode.TotalLengthTicks; } else if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Stopped && BackgroundAudioPlayer.Instance.Position.Ticks == 0) { playpos = CurrentlyPlayingEpisode.TotalLengthTicks; } else { try { playpos = BackgroundAudioPlayer.Instance.Position.Ticks; } catch (Exception) { Debug.WriteLine("Warning: Player didn't return us a play pos!"); } } CurrentlyPlayingEpisode.SavedPlayPos = playpos; CurrentlyPlayingEpisode.setNoPlaying(); using (var db = new PodcastSqlModel()) { PodcastEpisodeModel savingEpisode = db.Episodes.FirstOrDefault(ep => ep.EpisodeId == CurrentlyPlayingEpisode.EpisodeId); if (savingEpisode != null) { savingEpisode.SavedPlayPos = CurrentlyPlayingEpisode.SavedPlayPos; // Update play state to listened as appropriate. if (savingEpisode.isListened()) { savingEpisode.markAsListened(db.settings().IsAutoDelete); removeFromPlayqueue(savingEpisode); } else { savingEpisode.EpisodePlayState = CurrentlyPlayingEpisode.EpisodePlayState; } db.SubmitChanges(); PodcastSubscriptionsManager.getInstance().podcastPlaystateChanged(savingEpisode.PodcastSubscription); } } PodcastSubscriptionsManager.getInstance().podcastPlaystateChanged(CurrentlyPlayingEpisode.getSubscriptionModel()); handlerStoppedPlaying = OnPodcastStoppedPlaying; if (handlerStoppedPlaying != null) { OnPodcastStoppedPlaying(this, new EventArgs()); } // Cleanup CurrentlyPlayingEpisode = null; break; case PlayState.TrackReady: break; case PlayState.Unknown: // Unknown? WTF. break; } App.mainViewModels.PlayQueue = new System.Collections.ObjectModel.ObservableCollection <PlaylistItem>(); if (CurrentlyPlayingEpisode != null) { PodcastSubscriptionsManager.getInstance().podcastPlaystateChanged(CurrentlyPlayingEpisode.PodcastSubscriptionInstance); } }
public static void refreshEpisodesFromAudioAgent() { Debug.WriteLine("Refreshing episode information that has been updated from AudioPlayer."); using (var playlistdb = new PlaylistDBContext()) { int playlistItemsCount = 0; int listenedItemsCount = 0; List <PlaylistItem> playlistItems = playlistdb.Playlist.ToList(); playlistItemsCount = playlistItems.Count; using (var db = new PodcastSqlModel()) { bool deleteListened = false; if (playlistItems.Count > 0) { deleteListened = db.settings().IsAutoDelete; } foreach (PlaylistItem i in playlistItems) { PodcastEpisodeModel e = db.Episodes.FirstOrDefault(ep => ep.EpisodeId == i.EpisodeId); if (e == null) { Debug.WriteLine("Warning: Could not fetch episode with ID: " + i.EpisodeId); continue; } if (i.SavedPlayPosTick > 0) { Debug.WriteLine("Updating episode '" + e.EpisodeName + "' playpos to: " + i.SavedPlayPosTick); e.SavedPlayPos = i.SavedPlayPosTick; try { db.SubmitChanges(); } catch (ChangeConflictException) { Debug.WriteLine("ChangeConflictException: Could not submit changes"); } // Update play state to listened as appropriate. if (e.isListened()) { e.markAsListened(deleteListened); PodcastPlaybackManager.getInstance().removeFromPlayqueue(e); listenedItemsCount++; } e.PodcastSubscription.reloadPartiallyPlayedEpisodes(); e.PodcastSubscription.reloadUnplayedPlayedEpisodes(); } } } // If all items in the play queue were listened, then we shouldn't show an episodep laying anymore. if (playlistItemsCount == listenedItemsCount) { PodcastPlaybackManager.getInstance().CurrentlyPlayingEpisode = null; } } }