/// <summary> /// Sorts the play queue into a random order. /// </summary> public void ShufflePlayQueue() { bool wasPlayingBefore = false; if (ToccataModel.MediaPlayerIsPlaying()) { wasPlayingBefore = true; } this.Stop(); // Construct a list of all the items in the play queue (we will pull random items off it, in the code below). List <PlayableItem> playableItemList = new List <PlayableItem>((IEnumerable <PlayableItem>) this.PlayQueue); this.PlayQueue.Clear(); Random random = new Random(); while ((uint)playableItemList.Count > 0U) { int index = 0; if (playableItemList.Count > 1) { index = random.Next(0, playableItemList.Count); // pick a random index in the list } PlayableItem playableItem = playableItemList[index]; // add the random item to the play queue, and remove it from the list. playableItemList.RemoveAt(index); this.PlayQueue.Add(playableItem); } if (wasPlayingBefore) { this.StartPlayingIfAppropriate(); } }
/// <summary> /// Stop playing the current track and move it to the history, then start playing the track that is now the top of the play queue. This is the right /// metyhod to call when the 'Next' button is tapped. /// </summary> public void Next() { if (this.PlayQueue.Count <= 1) // if there is no queued track, do nothing. If there is only one queued track, then there is no next track, so do nothing. { return; } // This is a bit nasty and hacky - if the next button is pressed repeatedly in quick succession, the player // may be transiently paused when the button is pressed for the second, third, ... etc time, as a result of // an earlier press. So our logic for checking whether the player is playing now, and should therefore be // restarted after we have skipped forward a track, needs to be careful to ignore those transient state changes. // Hence this code, which says we will examine the player state only if the button hasn't already been pressed // in the recent past. if (_dtNextPressed.AddMilliseconds(500) < DateTime.Now) { _wasPlayingBeforeNextPressed = ToccataModel.MediaPlayerIsPlaying(); } _dtNextPressed = DateTime.Now; this.Stop(); this.history.Insert(0, this.PlayQueue[0]); // Take the currently playing track (the one at the top of the queue), add it to the history this.PlayQueue.RemoveAt(0); // and remove it from the play queue. if (_wasPlayingBeforeNextPressed) { this.StartPlayingIfAppropriate(); } }
/// <summary> /// Remove an item from the play queue. If it's the top item of the queue, and currently playing, then stop playing it, and /// start playing the next on the queue. /// </summary> /// <param name="i">item to remove</param> public void DeleteFromQueue(PlayableItem i) { if (this.PlayQueue.Count <= 0) { return; } bool wasPlayingBefore = false; if (ToccataModel.MediaPlayerIsPlaying()) { wasPlayingBefore = true; } if (this.PlayQueue[0] == i) // if we're removing the top item of the queue (i.e. the current track) { this.Stop(); // then hard-stop playback } this.PlayQueue.Remove(i); if (wasPlayingBefore) { this.StartPlayingIfAppropriate(); } }
/// <summary> /// Stops playing the current track (if it's playing) and goes back to the start of the previous track we were playing. /// </summary> public void Back() { if (this.history.Count <= 0) { return; } // This is a bit nasty and hacky - if the back button is pressed repeatedly in quick succession, the player // may be transiently paused when the button is pressed for the second, third, ... etc time, as a result of // an earlier press. So our logic for checking whether the player is playing now, and should therefore be // restarted after we have skipped back a track, needs to be careful to ignore those transient state changes. // Hence this code, which says we will examine the player state only if the button hasn't already been pressed // in the recent past. if (_dtBackPressed.AddMilliseconds(500) < DateTime.Now) { _wasPlayingBeforeBackPressed = ToccataModel.MediaPlayerIsPlaying(); } _dtBackPressed = DateTime.Now; this.Stop(); this.PlayQueue.Insert(0, this.history[0]); // Put the top track from ths history list onto the top of the play queue, this.history.RemoveAt(0); // and remove it from the history list if (_wasPlayingBeforeBackPressed) { this.StartPlayingIfAppropriate(); } }
private static bool _SliderIsBeingManipulated = false; // set to true while the user is manually manipulating the slider private void slPosition_SliderManipulationStarted(object sender, EventArgs e) { MainPage._SliderIsBeingManipulated = true; if (ToccataModel.MediaPlayerIsPlaying()) MainPage._WasPlayingWhenManipulationStarted = true; MainViewModel.Instance.Pause(); }