private void saveEpisodeInfoToDB(PodcastEpisodeModel m_currentEpisodeDownload)
        {
            if (m_currentEpisodeDownload == null)
            {
                return;
            }

            m_currentEpisodeDownload.StoreProperty <PodcastEpisodeModel.EpisodeDownloadStateEnum>("EpisodeDownloadState", m_currentEpisodeDownload.EpisodeDownloadState);
            m_currentEpisodeDownload.StoreProperty <String>("EpisodeFile", m_currentEpisodeDownload.EpisodeFile);
        }
Esempio n. 2
0
        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);
            }
        }