windows-phone-8windows-phoneaudio-playerbackground-audioknown-folders

Error when adding music files to the AudioTrack list - WP8


I'm new to on the app development scene, and I'm building a music player for Windows Phone. The problem is when I try to import music from the local Music Library to my app.

I have an AudioPlaybackAgent and a List of music that it will play. Here's the list:

public static List<AudioTrack> _playList = new List<AudioTrack>
        {
            new AudioTrack(new Uri("Chama os Mulekes.mp3", UriKind.Relative), 
                    "Chama os Mulekes", 
                    "ConeCrewDiretoria", 
                    "Com os Neurônios Evoluindo", 
                    new Uri("https://lh6.googleusercontent.com/-_d2oha1QzNM/UhjYxv3XsLI/AAAAAAAAAWk/QyWgG7ilTAs/s0-d/Cone-Crew-Diretoria-Com-os-neuronios-evoluindo.jpg", UriKind.Absolute))
        };

I added a button to the interface that when clicked it imports the local music library files to that playlist. Here's the code:

private async void ImportButton_Click(object sender, RoutedEventArgs e)
        {
            var musicLibrary = await KnownFolders.MusicLibrary.GetFilesAsync();
            foreach (StorageFile _file in musicLibrary)
            {
                var musicTags = await _file.Properties.GetMusicPropertiesAsync();
                MusicPlusAgent.AudioPlayer._playList.Add
                    (new AudioTrack (new Uri(_file.Path, UriKind.RelativeOrAbsolute),
                        musicTags.Title,
                        musicTags.Artist,
                        musicTags.Album,
                        null));
            }
        }

When run the app and touch the play button, the preset music plays fine, but when I touch the import button the app crashes. Any idea on what's wrong here?

Ps. Sorry for the bad english.


Solution

  • The Windows Phone BackgroundAudioPlayer API pre-dates the WinRT StorageFolder API and thus it doesn't work with those types of files (but is still available for back-compat). BackgroundAudioPlayer (and thus AudioTrack) is designed to play HTTP URLs and files from your local isolated storage folder. See this MSDN walkthrough for more information.

    Note that you can't use the XNA-based MediaLibrary APIs either to implement your own background-audio player. It is assumed that the user will use the built-in music player to play those files.