private void MenuItemDelete_Click(object sender, RoutedEventArgs e) { PodcastEpisodeModel podcastEpisode = (sender as MenuItem).DataContext as PodcastEpisodeModel; podcastEpisode.deleteDownloadedEpisode(); PodcastSubscriptionsManager.getInstance().removedPlayableEpisode(podcastEpisode); using (var db = new PodcastSqlModel()) { m_episodeModel = db.episodeForEpisodeId(m_episodeModel.EpisodeId); } this.DataContext = null; this.DataContext = m_episodeModel; }
private void processOngoingTransfer() { // If key exists, we know we have need to process a download request. if (m_applicationSettings.Contains(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID)) { Debug.WriteLine("Found a episode download that we need to process."); int downloadingEpisodeId = (int)m_applicationSettings[App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID]; m_currentEpisodeDownload = null; using (var db = new PodcastSqlModel()) { m_currentEpisodeDownload = db.episodeForEpisodeId(downloadingEpisodeId); } if (m_currentEpisodeDownload == null) { Debug.WriteLine("Something went wrong. Got NULL episode when asking for episode id " + downloadingEpisodeId); m_applicationSettings.Remove(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID); m_applicationSettings.Save(); return; } if (BackgroundTransferService.Requests.Count() > 0) { // Found an ongoing request. Debug.WriteLine("Found an ongoing transfer..."); m_currentBackgroundTransfer = BackgroundTransferService.Requests.ElementAt(0); m_currentBackgroundTransfer.TransferStatusChanged += new EventHandler <BackgroundTransferEventArgs>(backgroundTransferStatusChanged); m_currentEpisodeDownload.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Downloading; m_currentEpisodeDownload.DownloadRequest = m_currentBackgroundTransfer; m_episodeDownloadQueue.Enqueue(m_currentEpisodeDownload); ProcessTransfer(m_currentBackgroundTransfer); saveEpisodeInfoToDB(m_currentEpisodeDownload); } else { // No ongoing requests found. Then we need to process a finished request. // Probably happened in the background while we were suspended. Debug.WriteLine("Found a completed request."); updateEpisodeWhenDownloaded(m_currentEpisodeDownload); m_applicationSettings.Remove(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID); m_applicationSettings.Save(); } } }
private void updateEpisodeWhenDownloaded(PodcastEpisodeModel episode) { Debug.WriteLine("Updating episode information for episode when download completed: " + episode.EpisodeName); episode.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Downloaded; using (var db = new PodcastSqlModel()) { Debug.WriteLine(" * Downloaded file name: " + episode.EpisodeFile); PodcastEpisodeModel e = db.episodeForEpisodeId(episode.EpisodeId); e.EpisodeFile = episode.EpisodeFile; e.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Downloaded; e.EpisodePlayState = PodcastEpisodeModel.EpisodePlayStateEnum.Downloaded; db.SubmitChanges(); PodcastSubscriptionsManager.getInstance().podcastPlaystateChanged(e.PodcastSubscription); } }
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; CurrentlyPlayingEpisode.setPlaying(); } if (CurrentlyPlayingEpisode.TotalLengthTicks == 0) { CurrentlyPlayingEpisode.TotalLengthTicks = BackgroundAudioPlayer.Instance.Track.Duration.Ticks; using (var db = new PodcastSqlModel()) { PodcastEpisodeModel episode = db.episodeForEpisodeId(CurrentlyPlayingEpisode.EpisodeId); if (episode == null) { Debug.WriteLine("Warning: Got NULL episode from DB when trying to update this episode."); return; } episode.TotalLengthTicks = CurrentlyPlayingEpisode.TotalLengthTicks; db.SubmitChanges(); PodcastSubscriptionsManager.getInstance().podcastPlaystateChanged(episode.PodcastSubscription); } } break; case PlayState.Paused: BackgroundAudioPlayer playerPaused = BackgroundAudioPlayer.Instance; if (CurrentlyPlayingEpisode != null) { CurrentlyPlayingEpisode = App.refreshEpisodeFromAudioAgent(CurrentlyPlayingEpisode); CurrentlyPlayingEpisode.EpisodePlayState = PodcastEpisodeModel.EpisodePlayStateEnum.Paused; using (var db = new PodcastSqlModel()) { PodcastEpisodeModel updatedEpisode = db.Episodes.FirstOrDefault(ep => ep.EpisodeId == CurrentlyPlayingEpisode.EpisodeId); updatedEpisode.EpisodePlayState = CurrentlyPlayingEpisode.EpisodePlayState; db.SubmitChanges(); } handlerStoppedPlaying = OnPodcastStoppedPlaying; if (handlerStoppedPlaying != null) { OnPodcastStoppedPlaying(this, new EventArgs()); } } 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.PodcastSubscriptionInstance); 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); } }
private void processOngoingTransfer() { // If key exists, we know we have need to process a download request. if (m_applicationSettings.Contains(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID)) { Debug.WriteLine("Found a episode download that we need to process."); int downloadingEpisodeId = (int)m_applicationSettings[App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID]; m_currentEpisodeDownload = null; using (var db = new PodcastSqlModel()) { m_currentEpisodeDownload = db.episodeForEpisodeId(downloadingEpisodeId); } if (m_currentEpisodeDownload == null) { Debug.WriteLine("Something went wrong. Got NULL episode when asking for episode id " + downloadingEpisodeId); m_applicationSettings.Remove(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID); m_applicationSettings.Save(); return; } if (BackgroundTransferService.Requests.Count() > 0) { // Found an ongoing request. Debug.WriteLine("Found an ongoing transfer..."); m_currentBackgroundTransfer = BackgroundTransferService.Requests.ElementAt(0); m_currentBackgroundTransfer.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(backgroundTransferStatusChanged); m_currentEpisodeDownload.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Downloading; m_currentEpisodeDownload.DownloadRequest = m_currentBackgroundTransfer; m_episodeDownloadQueue.Enqueue(m_currentEpisodeDownload); ProcessTransfer(m_currentBackgroundTransfer); saveEpisodeInfoToDB(m_currentEpisodeDownload); } else { // No ongoing requests found. Then we need to process a finished request. // Probably happened in the background while we were suspended. Debug.WriteLine("Found a completed request."); updateEpisodeWhenDownloaded(m_currentEpisodeDownload); m_applicationSettings.Remove(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID); m_applicationSettings.Save(); } } }
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; CurrentlyPlayingEpisode.setPlaying(); } if (CurrentlyPlayingEpisode.TotalLengthTicks == 0) { CurrentlyPlayingEpisode.TotalLengthTicks = BackgroundAudioPlayer.Instance.Track.Duration.Ticks; using (var db = new PodcastSqlModel()) { PodcastEpisodeModel episode = db.episodeForEpisodeId(CurrentlyPlayingEpisode.EpisodeId); if (episode == null) { Debug.WriteLine("Warning: Got NULL episode from DB when trying to update this episode."); return; } episode.TotalLengthTicks = CurrentlyPlayingEpisode.TotalLengthTicks; db.SubmitChanges(); PodcastSubscriptionsManager.getInstance().podcastPlaystateChanged(episode.PodcastSubscription); } } break; case PlayState.Paused: BackgroundAudioPlayer playerPaused = BackgroundAudioPlayer.Instance; if (CurrentlyPlayingEpisode != null) { CurrentlyPlayingEpisode = App.refreshEpisodeFromAudioAgent(CurrentlyPlayingEpisode); CurrentlyPlayingEpisode.EpisodePlayState = PodcastEpisodeModel.EpisodePlayStateEnum.Paused; using (var db = new PodcastSqlModel()) { PodcastEpisodeModel updatedEpisode = db.Episodes.FirstOrDefault(ep => ep.EpisodeId == CurrentlyPlayingEpisode.EpisodeId); updatedEpisode.EpisodePlayState = CurrentlyPlayingEpisode.EpisodePlayState; db.SubmitChanges(); } handlerStoppedPlaying = OnPodcastStoppedPlaying; if (handlerStoppedPlaying != null) { OnPodcastStoppedPlaying(this, new EventArgs()); } } 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.PodcastSubscriptionInstance); 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); } }