/// <summary> /// Saves the current play queue to a file. /// </summary> /// <param name="f">the file to save to</param> /// <returns>true if it managed to save OK</returns> public async Task <bool> SavePlayQueue(StorageFile f) { try { List <string> paths = new List <string>(); foreach (PlayableItem play in (Collection <PlayableItem>) this.PlayQueue) { PlayableItem i = play; paths.Add(i.storage.Path); } using (Stream fr1 = await WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync((IStorageFile)f)) { using (StreamWriter tr = new StreamWriter(fr1)) { foreach (string str in paths) { string s = str; tr.WriteLine(s); // the file holds one file path per line } } } return(true); } catch (Exception) { return(false); } }
/// <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> /// 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> /// Play the chosen track once the current one has finished. This is the right method to call when the 'Play Next' context /// menu item is tapped. /// </summary> /// <param name="i">the item to play next</param> public void PlayNext(PlayableItem i) { if (this.PlayQueue.Count <= 1 || this.PlayQueue[0] == i) // if the queue has one or less items, 'play next' is meaningless. If the chosen item is already playing, there is nothing to do. { return; } this.PlayQueue.Remove(i); // Remove it from wherever it was in the queue this.PlayQueue.Insert(1, i); // and insert it below the top item. }
/// <summary> /// Stop playing whatever we are playing, and start playing the chosen track immediately. This is trhe right method to call /// when the 'Play Now' context menu item is tapped. /// </summary> /// <param name="i">the item to start playing</param> public void PlayNow(PlayableItem i) { if (this.PlayQueue.Count <= 1 || this.PlayQueue[0] == i) // if the queue has one or less items, 'play next' is meaningless. If the chosen item is already playing, there is nothing to do. { return; } this.Stop(); // stop playing the current item this.PlayQueue.Remove(i); // Remove the chosen item from wherever it was in the queue this.PlayQueue.Insert(0, i); // and put it at the top of the play queue. this.StartPlayingIfAppropriate(); // and start playing. }