private SaveAsAsync ( [ saveLocation, [ desiredName, [ option ) : IAsyncOperation |
||
saveLocation | [ | |
desiredName | [ | |
option | [ | |
return | IAsyncOperation |
//When adding a song to new playlist. private async void NewPlaylist_click(object sender, RoutedEventArgs e) { var buttonTag = ((Button)sender).Tag.ToString(); Debug.WriteLine(buttonTag); PlaylistList.Items.Clear(); ContentDialogResult result = await NewPlayListDialog.ShowAsync(); if (result == ContentDialogResult.Primary) { Windows.Media.Playlists.Playlist playlist = new Windows.Media.Playlists.Playlist(); StorageFolder sf = KnownFolders.MusicLibrary; string name = PlaylistName.Text; NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting; PlaylistFormat format = PlaylistFormat.WindowsMedia; StorageFile storageFile = await StorageFile.GetFileFromPathAsync(buttonTag); playlist.Files.Add(storageFile); try { StorageFile savedFile = await playlist.SaveAsAsync(sf, name, collisionOption, format); LoadPlaylistsView(); LoadPlaylists(); } catch (Exception error) { Debug.WriteLine(error.StackTrace); } } }
/// <summary> /// Creates a playlist with the audio picked by the user in the FilePicker /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async private void PickAudioButton_Click(object sender, RoutedEventArgs e) { FileOpenPicker picker = MainPage.CreateFilePicker(MainPage.audioExtensions); IReadOnlyList<StorageFile> files = await picker.PickMultipleFilesAsync(); if (files.Count > 0) { Playlist playlist = new Playlist(); foreach (StorageFile file in files) { playlist.Files.Add(file); } StorageFolder folder = KnownFolders.MusicLibrary; string name = "Sample"; NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting; PlaylistFormat format = PlaylistFormat.WindowsMedia; try { StorageFile savedFile = await playlist.SaveAsAsync(folder, name, collisionOption, format); this.rootPage.NotifyUser(savedFile.Name + " was created and saved with " + files.Count + " files.", NotifyType.StatusMessage); } catch (Exception error) { rootPage.NotifyUser(error.Message, NotifyType.ErrorMessage); } } else { rootPage.NotifyUser("No files picked.", NotifyType.ErrorMessage); } }
public async Task ExportAsync(Playlist list, StorageFolder destFolder, string fileName) { if (fileName.ToLower().EndsWith(".wpl")) { fileName = fileName.Remove(fileName.Length - 4, 4); } var export = new SystemPlaylist(); foreach (var item in list.Items) { var file = await NativeMethods.GetStorageFileFromPathAsync(item.Path) as StorageFile; if (file != null) { export.Files.Add(file); } } await export.SaveAsAsync(destFolder, fileName, NameCollisionOption.ReplaceExisting, PlaylistFormat.WindowsMedia); }
//When adding a song to existing playlist. private async void ExistingPlaylist_Click(object sender, RoutedEventArgs e) { var buttonTag = ((Button)sender).Tag.ToString(); Debug.WriteLine("Inside dialog:"); PlaylistList.Items.Clear(); foreach (var v in PlaylistsList) { v.Name = v.Name.Substring(0, v.Name.LastIndexOf('.')); PlaylistList.Items.Add(v); Debug.WriteLine(v.Name); } Debug.WriteLine("Ends for loop:"); ExistingPlayListDialog.IsPrimaryButtonEnabled = false; ContentDialogResult result = await ExistingPlayListDialog.ShowAsync(); if (result == ContentDialogResult.Primary) { Windows.Media.Playlists.Playlist playlist = new Windows.Media.Playlists.Playlist(); StorageFolder sf = KnownFolders.MusicLibrary; IReadOnlyList <StorageFile> fileList = await sf.GetFilesAsync(); var playlistFile = fileList.Where(f => f.Name == clickedPlaylist.Name).FirstOrDefault(); playlist = await Windows.Media.Playlists.Playlist.LoadAsync(playlistFile); NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting; PlaylistFormat format = PlaylistFormat.WindowsMedia; StorageFile storageFile = await StorageFile.GetFileFromPathAsync(buttonTag); playlist.Files.Add(storageFile); try { StorageFile savedFile = await playlist.SaveAsAsync(sf, playlistFile.Name.Replace(".wpl", ""), collisionOption, format); Debug.WriteLine("Edited successfully"); } catch (Exception error) { Console.WriteLine(error.StackTrace); Debug.WriteLine("Something went wrong:" + error.StackTrace); } } }
private void SaveList() { var result = fileDialogService.ShowSaveFileDialog(shellService.ShellView, savePlaylistFileType); if (!result.IsValid) { return; } var playlist = new Playlist(); try { foreach (var item in PlaylistManager.Items) { var file = StorageFile.GetFileFromPathAsync(item.MusicFile.FileName).GetResult(); playlist.Files.Add(file); } } catch (Exception ex) { Logger.Error("SaveList GetStorageFiles: {0}", ex); shellService.ShowError(ex, Resources.CouldNotSavePlaylistBecauseMissingFiles); return; } try { var targetFolder = StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(result.FileName)).GetResult(); var name = Path.GetFileNameWithoutExtension(result.FileName); playlist.SaveAsAsync(targetFolder, name, NameCollisionOption.ReplaceExisting, PlaylistFormat.M3u).Wait(); } catch (Exception ex) { Logger.Error("SaveList SaveAs: {0}", ex); shellService.ShowError(ex, Resources.CouldNotSavePlaylist); } }
//Handels playlist list item click event to show songds of the playlist in dialog box. private async void PlaylistList_ItemClick(object sender, ItemClickEventArgs e) { queue.Clear(); PlaylistSongs.Items.Clear(); const ThumbnailMode thumbnailMode = ThumbnailMode.MusicView; Model.Playlist playlistToShow = (Model.Playlist)e.ClickedItem; var fileToShow = fileList.Where(f => f.Name == playlistToShow.Name).FirstOrDefault(); Windows.Media.Playlists.Playlist playlist = await Windows.Media.Playlists.Playlist.LoadAsync(fileToShow); foreach (var s in playlist.Files) { var af = songFileList.Where(sf => sf.Name == s.Name).FirstOrDefault(); const uint size = 100; using (StorageItemThumbnail thumbnail = await s.GetThumbnailAsync(thumbnailMode, size)) { if (thumbnail != null && (thumbnail.Type == ThumbnailType.Image || thumbnail.Type == ThumbnailType.Icon)) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(thumbnail); Model.MediaFile o1 = new Model.AudioFile(); Image i = new Image(); MusicProperties musicProperties = await s.Properties.GetMusicPropertiesAsync(); i.Source = bitmapImage; o1.Thumb = i; o1.Title = s.Name; if (musicProperties.Title != "") { o1.Title = musicProperties.Title; } o1.Name = s.Name; o1.Path = s.Path; PlaylistSongs.Items.Add(o1); } } } SonglistView.Title = fileToShow.Name.Replace(".wpl", ""); SonglistView.IsPrimaryButtonEnabled = true; SonglistView.PrimaryButtonText = "Save"; PlaylistSongs.CanDragItems = true; PlaylistSongs.CanReorderItems = true; PlaylistSongs.AllowDrop = true; ContentDialogResult contentDialogResult = await SonglistView.ShowAsync(); if (contentDialogResult == ContentDialogResult.Primary) { playlist.Files.Clear(); StorageFolder sf = KnownFolders.MusicLibrary; NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting; PlaylistFormat format = PlaylistFormat.WindowsMedia; foreach (Model.MediaFile item in PlaylistSongs.Items) { StorageFile storageFile = await StorageFile.GetFileFromPathAsync(item.Path); playlist.Files.Add(storageFile); Debug.WriteLine(item.Name); } StorageFile savedFile = await playlist.SaveAsAsync(sf, fileToShow.Name.Replace(".wpl", ""), collisionOption, format); } else if (contentDialogResult == ContentDialogResult.Secondary) { Play_From_Playlist(playlistToShow.Name); } }