0

The Microsoft W10 Universal apps Background Audio sample can play a list of .wma files stored in ///Assets, like this:

var song2 = new SongModel();
song2.Title = "Ring 2";
song2.MediaUri = new Uri("ms-appx:///Assets/Media/Ring02.wma");
song2.AlbumArtUri = new Uri("ms-appx:///Assets/Media/Ring02.jpg");
playlistView.Songs.Add(song2);

But I can't get the program to play .wma files stored on disk. I tried to select a file using the FileOpenPicker, assign it to StorageFile file and then:

if (file != null)
{
Uri uri = new Uri(file.Path);
song2.MediaUri = uri;
}

or by (temporary) placing it in the Pictures library (which I checked in the capabilities) which I thought I could access like this but either that's not the case or it doesn't work (and most likely both):

string name = "ms-appdata:///local/images/SomeSong.wma";
Uri uri = new Uri(name, UriKind.Absolute);
song1.MediaUri = uri;

Only the original ///Assets WMA is audible.

What should I change? And how can I convert a KnownFolders directory to a Uri?

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
Dick
  • 433
  • 5
  • 13

1 Answers1

0

The Background Audio sample uses MediaSource.CreateFromUri method to create media source. While using this method, the parameter can only be set to the Uniform Resource Identifier (URI) of a file that is included with the app or the URI of a file on the network. To set the source to a file retrieved from the local system by using a FileOpenPicker object, we can use MediaSource.CreateFromStorageFile method. And whenever our app accesses a file or folder through a picker, we can add it to app's FutureAccessList or MostRecentlyUsedList to keep track of it.

For example, after we get the StorageFile from FileOpenPicker, we can add it to FutureAccessList and store the token that the app can use later to retrieve the storage item in app's local settings like:

if (file != null)
{
    var token = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);

    ApplicationData.Current.LocalSettings.Values["song1"] = token;
}

For more info about FutureAccessList, please see Track recently used files and folders.

Then in BackgroundAudioTask, I changed CreatePlaybackList method to replace the original like:

private async void CreatePlaybackList(IEnumerable<SongModel> songs)
{
    // Make a new list and enable looping
    playbackList = new MediaPlaybackList();
    playbackList.AutoRepeatEnabled = true;

    // Add playback items to the list
    foreach (var song in songs)
    {
        MediaSource source;
        //Replace Ring 1 to the song we select
        if (song.Title.Equals("Ring 1"))
        {
            var file = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(ApplicationData.Current.LocalSettings.Values["song1"].ToString());
            source = MediaSource.CreateFromStorageFile(file);
        }
        else
        {
            source = MediaSource.CreateFromUri(song.MediaUri);
        }
        source.CustomProperties[TrackIdKey] = song.MediaUri;
        source.CustomProperties[TitleKey] = song.Title;
        source.CustomProperties[AlbumArtKey] = song.AlbumArtUri;
        playbackList.Items.Add(new MediaPlaybackItem(source));
    }

    // Don't auto start
    BackgroundMediaPlayer.Current.AutoPlay = false;

    // Assign the list to the player
    BackgroundMediaPlayer.Current.Source = playbackList;

    // Add handler for future playlist item changes
    playbackList.CurrentItemChanged += PlaybackList_CurrentItemChanged;
}

This is just a simple sample, you may need to change the SongModel and some other code to implement your own player. For more info about Background Audio, you can also refer to The Basics of Background Audio. Besides, with Windows 10, version 1607, significant improvements were made to the media playback APIs, including a simplified single-process design for background audio. You can see Play media in the background to check the new feature.

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49