Beispiel #1
0
        /// <summary>
        /// Start playing, from the start of the track, if: (1) there is a queued track to play and (2) the player is in the hard-stopped state at the moment
        /// </summary>
        public void StartPlayingIfAppropriate()
        {
            if (this.PlayQueue.Count == 0 || !ToccataModel.MediaPlayerHasNoSource())
            {
                return;
            }

            ToccataModel.Play(this.PlayQueue[0].storage); // start playing the track that is at the top of the queue.
        }
Beispiel #2
0
 /// <summary>
 /// Starts or restarts playback.
 /// </summary>
 public void Play()
 {
     if (ToccataModel.MediaPlayerHasNoSource()) // if the player has been hard-stopeed, then ...
     {
         if (this.PlayQueue.Count <= 0)
         {
             return;
         }
         ToccataModel.Play(this.PlayQueue[0].storage); // ... play the top track on the queue
     }
     else
     {
         ToccataModel.Play(); // ... else play whatever is currently assigned as the player's source.
     }
 }
Beispiel #3
0
        //private DispatcherTimer _dtimer = null;
        private async void ThrottledPlaybackStateChanged(MediaPlayer player)
        {
            if (_playbackStateChangePending)
            {
                // A change has already been noted, and the 'state changed' handler will get called anyway some time within the next 100ms
                return;
            }
            else
            {
                _playbackStateChangePending = true;

                await Task.Delay(100); // allow 100ms for subsequent state changes to come in

                _playbackStateChangePending = false;

                //
                // Now adjust the appearance of the Pause/Play button as needed, and kick off playback of the next track in the queue, if there is one.
                //
                if (player.PlaybackSession == null) // error - never started
                {
                }
                else if (player.PlaybackSession.PlaybackState == MediaPlaybackState.Paused)
                {
                    bool trackFinished = false; //at the end of a track?
                    if (player.PlaybackSession.Position != TimeSpan.Zero && player.PlaybackSession.Position.Add(TimeSpan.FromSeconds(1)) >= player.PlaybackSession.NaturalDuration)
                    {
                        // Position is not at the start of the media, and is equal/near to the duration of the player's media,
                        // This means we are at the end of a track
                        trackFinished = true;
                    }

                    MainPage.SetPlayButtonAppearance(false);              // Set the Play button to display a "play" label, and do 'play' when tapped.

                    if (trackFinished)                                    // paused, and at the end of a track
                    {
                        MainPage.SetNowPlaying("");                       // "***FINISHED: " + player.PlaybackSession.PlaybackState.ToString() + " " + player.PlaybackSession.Position.TotalSeconds.ToString() + "/" + player.PlaybackSession.NaturalDuration.TotalSeconds.ToString()); // set the text label at the bottom of the slider to blank.

                        if (PlayQueue.Count > 0)                          // (this is just defensive coding, it should always be true)
                        {
                            PlayQueue.RemoveAt(0);                        // We've finished this track, so remove it from the top of the play queue.
                        }
                        if (PlayQueue.Count > 0)                          // if there is now a track at the top of the queue ...
                        {
                            ToccataModel.Play(this.PlayQueue[0].storage); // ... start playing it.
                        }
                        else
                        {
                            ToccataModel.Stop(); // ... otherwise, we should hard-stop the player, leaving it ready to play something, when something is queued up.
                        }
                    }
                    else // paused but not finished the track - e.g. because the user tapped the Pause button.
                    {
                        // MainPage.SetNowPlaying("***PAUSED: " + player.PlaybackSession.PlaybackState.ToString()+" "+ player.PlaybackSession.Position.TotalSeconds.ToString()+"/"+ player.PlaybackSession.NaturalDuration.TotalSeconds.ToString());
                    }
                }
                else // playing, buffering, opening, none, or whatever
                {
                    if (player.PlaybackSession.PlaybackState == MediaPlaybackState.Playing)
                    {
                        MainPage.SetPlayButtonAppearance(true); // Set the Play button to display a "pause" label, and do 'pause' when tapped.
                    }
                    else // could be 'buffering', 'opening' or 'none' - anyhow, it's a state in which the media is not (yet) playing.
                    {
                        MainPage.SetPlayButtonAppearance(false); // Set the Play button to display a "play" label, and do 'play' when tapped.
                    }

                    if (PlayQueue.Count > 0) // (this is just defensive coding, it should always be true)
                    {
                        MainPage.SetNowPlaying(PlayQueue[0].DisplayName + " (" + PlayQueue[0].storage.Path + ")");
                    }
                }
            }
        }