Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone, I am try to make a Win 8.1 Music Player Metro App. The app must have a playlist that shows all the songs in a ListBox. When I try to load a playlist a UnhandledException is thrown. The same with when I try to add a song to the playlist.

Please I really need help.
The code I have been trying is below.

C#
public sealed partial class MainPage : Page
{

    public static readonly string[] audioExtensions = new string[] { ".wma", ".mp3", ".mp2", ".aac", ".adt", ".adts", ".m4a" };
    public static readonly string[] playlistExtensions = new string[] { ".m3u", ".wpl", ".zpl" };
    Playlist playlist = new Playlist();

    public MainPage()
    {
        this.InitializeComponent();

    }

    async private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            playlist = await PickPlaylistAsync();

            if (playlist != null)
            {
                foreach (StorageFile file in playlist.Files)
                {
                    MusicProperties properties = await file.Properties.GetMusicPropertiesAsync();
                    lstPlaylist.Items.Add(properties.Title);
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    async private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            FileOpenPicker picker = CreateFilePicker(audioExtensions);
            IReadOnlyList<StorageFile> files = await picker.PickMultipleFilesAsync();

            if (files != null && files.Count > 0)
            {
                foreach (StorageFile file in files)
                {
                    playlist.Files.Add(file);

                }
                await playlist.SaveAsync();
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    async public static Task<Playlist> PickPlaylistAsync()
    {
        FileOpenPicker picker = CreateFilePicker(playlistExtensions);
        StorageFile file = await picker.PickSingleFileAsync();

        Playlist playlist = null;
        if (file != null)
        {
            return await Playlist.LoadAsync(file);
        }

        return playlist;
    }

    public static FileOpenPicker CreateFilePicker(string[] extensions)
    {
        FileOpenPicker picker = new FileOpenPicker();
        picker.SuggestedStartLocation = PickerLocationId.MusicLibrary;

        foreach (string extension in extensions)
        {
            picker.FileTypeFilter.Add(extension);
        }
        return picker;
    }

    async private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (PlaylistName.Text != "")
            {
                StorageFile savedFile = await playlist.SaveAsAsync(KnownFolders.MusicLibrary, PlaylistName.Text, NameCollisionOption.ReplaceExisting, PlaylistFormat.WindowsMedia);
            }
            else
            {

            }

        }
        catch (Exception)
        {
            throw;
        }

    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900