Inheritance: IPlaylist
Beispiel #1
0
        // Play list of songs from playlist file.
        private async void Play_From_Playlist(string name)
        {
            var fileToPlay = fileList.Where(f => f.Name == name).FirstOrDefault();

            if (fileToPlay != null)
            {
                queue.Clear();
                Windows.Media.Playlists.Playlist playlist = await Windows.Media.Playlists.Playlist.LoadAsync(fileToPlay);

                MediaPlaybackList mediaPlaybackList = new MediaPlaybackList();
                foreach (var f in playlist.Files)
                {
                    var af = songFileList.Where(sf => sf.Name == f.Name).FirstOrDefault();
                    queue.Add(af);
                    mediaPlaybackList.Items.Add(new MediaPlaybackItem(MediaSource.CreateFromStorageFile(f)));
                }
                if (mediaPlaybackList.Items.Count != 0)
                {
                    mediaElement.Source = mediaPlaybackList;
                    mediaPlaybackList.CurrentItemChanged += new TypedEventHandler <MediaPlaybackList, CurrentMediaPlaybackItemChangedEventArgs>(Track_Changes);
                    mediaElement.MediaPlayer.Play();
                    currentPlaying = -1;
                }
            }
        }
Beispiel #2
0
        //Handles click event of shuffle button
        private async void ShuffleButton_Click(object sender, RoutedEventArgs e)
        {
            var buttonTag = ((Button)sender).Tag.ToString();

            if (fileList != null)
            {
                var fileToPlay = fileList.Where(f => f.Name == buttonTag).FirstOrDefault();
                if (fileToPlay != null)
                {
                    Windows.Media.Playlists.Playlist playlist = await Windows.Media.Playlists.Playlist.LoadAsync(fileToPlay);

                    MediaPlaybackList mediaPlaybackList = new MediaPlaybackList();
                    queue.Clear();
                    foreach (var item in playlist.Files)
                    {
                        var af = songFileList.Where(sf => sf.Name == item.Name).FirstOrDefault();
                        queue.Add(af);
                        mediaPlaybackList.Items.Add(new MediaPlaybackItem(MediaSource.CreateFromStorageFile(item)));
                    }

                    mediaPlaybackList.ShuffleEnabled = true;
                    IReadOnlyList <MediaPlaybackItem> items = mediaPlaybackList.ShuffledItems;
                    MediaPlaybackList newplaybacklist       = new MediaPlaybackList();
                    foreach (var item in items)
                    {
                        newplaybacklist.Items.Add(item);
                    }
                    if (newplaybacklist.Items.Count != 0)
                    {
                        mediaElement.Source = newplaybacklist;
                        mediaElement.MediaPlayer.Play();
                    }
                }
            }
        }
Beispiel #3
0
        //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);
            }
        }
        /// <summary>
        /// Loads a playlist picked by the user in the FilePicker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        async private void PickPlaylistButton_Click(object sender, RoutedEventArgs e)
        {
            PickAudioButton.IsEnabled = false;

            this.playlist = await rootPage.PickPlaylistAsync();

            if (this.playlist != null)
            {
                rootPage.NotifyUser("Playlist loaded.", NotifyType.StatusMessage);
            }

            PickAudioButton.IsEnabled = (this.playlist != null);

        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        //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);
                }
            }
        }
Beispiel #8
0
        //Loads playlist view.
        private async void LoadPlaylistsView()
        {
            PlaylistList1.Items.Clear();
            StorageFolder sf = KnownFolders.MusicLibrary;

            fileList = await sf.GetFilesAsync();

            List <Windows.Media.Playlists.Playlist> playlists = new List <Windows.Media.Playlists.Playlist>();

            foreach (StorageFile sfl in fileList)
            {
                if (sfl.FileType == ".wpl")
                {
                    Model.Playlist playlist = new Model.Playlist();
                    playlist.Name = sfl.Name;
                    playlist.Path = sfl.Path;
                    Windows.Media.Playlists.Playlist plst = await Windows.Media.Playlists.Playlist.LoadAsync(sfl);

                    playlists.Add(plst);
                    PlaylistList1.Items.Add(playlist);
                }
            }
        }
        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);
            }
        }
Beispiel #10
0
        //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);
            }
        }
 async public Task<bool> TrySavePlaylistAsync(Playlist playlist)
 {
     try
     {
         await playlist.SaveAsync();
         return true;
     }
     catch (Exception ex)
     {
         NotifyUser(ex.Message, NotifyType.ErrorMessage);
         return false;
     }
 }