/// <summary> /// Method which loads all music files from device /// </summary> private async void LoadMainAudioList() { mainPlaylist = new MusicPlaylist("Wszystkie utwory"); //Create a query which will get all music files from device QueryOptions musicFilesQuery = new QueryOptions(CommonFileQuery.OrderByTitle, new string[] { ".mp3", ".vma" }); musicFilesQuery.FolderDepth = FolderDepth.Deep; var musicFiles = await KnownFolders.MusicLibrary.CreateFileQueryWithOptions(musicFilesQuery).GetFilesAsync(); foreach (var musicFile in musicFiles) { mainPlaylist.Add(new MusicFile(musicFile)); } //Assign the list to the player SetCurrentPlaylist(mainPlaylist); Notify(); }
/// <summary> /// Gets playlists data from settings and creates playlists /// object /// </summary> private async void LoadAllPlaylists() { //Initialize this list from settings file with //id's of music files //Dictionary contains playlists names as keys and lists of //music files id as values Dictionary <string, List <string> > musicPlaylistsData = await settings.GetPlaylistsDataAsync(); if (musicPlaylists != null) { try { //Create playlist with given name and select //items from main playlist by id foreach (var playlistData in musicPlaylistsData) { MusicPlaylist playlist = new MusicPlaylist(playlistData.Key); foreach (var musicData in playlistData.Value) { if (musicData != "") { playlist.Add(mainPlaylist.musicFileList .Where(m => m.Id == musicData).Single()); } } musicPlaylists.Add(playlist); } } catch (Exception ex) { Debug.WriteLine(ex); } } isInitializationComplete = true; Notify(); }
private void MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs arguments) { object message; try { if (arguments.Data.TryGetValue(MessageType.Message.ToString(), out message)) { //It is a command, handle it Command command; if (Enum.TryParse(message.ToString(), out command)) { switch (command) { case Command.Play: Play(); break; case Command.Pause: Pause(); break; case Command.EnterBackground: isPlayerInBackground = true; break; case Command.LeaveBackground: isPlayerInBackground = false; isCommandFromForeground = false; SendMainPlaylistDataToForeground(); SendAllPlaylistsDataToForeground(); SendCurrentPlaylistToForeground(); SendCurrentItemToForeground(); break; } } } else if (arguments.Data.TryGetValue(MessageType.NewPlaylist.ToString(), out message)) { //New playlist created, handle it MusicPlaylist newPlaylist = new MusicPlaylist(message.ToString()); musicPlaylists.Add(newPlaylist); settings.SavePlaylists(musicPlaylists); } else if (arguments.Data.TryGetValue(MessageType.RemoveFromPlaylist.ToString(), out message)) { //Music file needs to be removed from playlist, handle it string[] dataToProcess = message.ToString().Split(dataSeparator.ToCharArray().Single()); if (dataToProcess.Count() != 2) { throw new Exception("Data frame incorrect"); } else { musicPlaylists.Where(m => m.Name == dataToProcess[0]) .Single().Remove(mainPlaylist.musicFileList.Where(m => m .Id == dataToProcess[1]).Single()); settings.SavePlaylists(musicPlaylists); } } else if (arguments.Data.TryGetValue(MessageType.AddToPlaylist.ToString(), out message)) { //Music file needs to be added to playlist, handle it string[] dataToProcess = message.ToString().Split(dataSeparator.ToCharArray().Single()); if (dataToProcess.Count() != 2) { throw new Exception("Data frame incorrect"); } else { musicPlaylists.Where(m => m.Name == dataToProcess[0]) .Single().Add(mainPlaylist.musicFileList.Where(m => m .Id == dataToProcess[1]).Single()); settings.SavePlaylists(musicPlaylists); } } else if (arguments.Data.TryGetValue(MessageType.RemovePlaylist.ToString(), out message)) { if (message.ToString().Equals("0")) { musicPlaylists.Clear(); } else { musicPlaylists.Remove(musicPlaylists.Where(m => m.Name == message.ToString()).Single()); } settings.SavePlaylists(musicPlaylists); } else if (arguments.Data.TryGetValue(MessageType.Playlist.ToString(), out message)) { //There is a playlist to add, handle it MusicPlaylist playlist = null; string dataSeparator = "\t"; string filesSeparator = "\n"; string[] playlistData = message.ToString().Split(filesSeparator.ToCharArray().Single()); //Validate that first element in data is name of playlist if (playlistData[0].Split(dataSeparator.ToCharArray().Single()).Length != 1) { throw new Exception("Incorrect data frame"); } else { if (musicPlaylists.Where(m => m .Name == playlistData[0]).Count() == 0) { playlist = new MusicPlaylist(playlistData[0]); //Remove first element which contains playlist name List <string> playlistList = playlistData.ToList(); playlistList.RemoveAt(0); foreach (var musicData in playlistList) { if (musicData != "") { playlist.Add(mainPlaylist.musicFileList .Where(m => m.Id == musicData).Single()); } } //Playlist is ready, add it to list musicPlaylists.Add(playlist); } else { throw new Exception("Playlist already exists"); } } } else if (arguments.Data.TryGetValue(MessageType.CurrentPlaylist.ToString(), out message)) { if (currentPlaylist.Name != message.ToString()) { if (mainPlaylist.Name == message.ToString()) { SetCurrentPlaylist(mainPlaylist); } else if (musicPlaylists.Where(m => m.Name == message.ToString()).Count() == 1) { SetCurrentPlaylist(musicPlaylists.Where(m => m.Name == message.ToString()).Single()); } } } else if (arguments.Data.TryGetValue(MessageType.MediaPlaybackItem.ToString(), out message)) { //User selected music item to play, handle it isCommandFromForeground = true; MusicFile musicFile = currentPlaylist.musicFileList .Where(m => m.Id == message.ToString()).Single(); if (musicFile != currentItem) { Play(musicFile); } } } catch (Exception ex) { Debug.WriteLine(ex); } }