简体   繁体   English

W10 Universal:如何使用Backgroundaudio从磁盘播放歌曲?

[英]W10 Universal: how to play a song from disk using Backgroundaudio?

The Microsoft W10 Universal apps Background Audio sample can play a list of .wma files stored in ///Assets, like this: Microsoft W10通用应用程序背景音频示例可以播放存储在/// Assets中的.wma文件列表,如下所示:

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. 但是我无法让该程序播放存储在磁盘上的.wma文件。 I tried to select a file using the FileOpenPicker, assign it to StorageFile file and then: 我试图使用FileOpenPicker选择一个文件,将其分配给StorageFile文件,然后:

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. 仅原始///资产WMA可以听见。

What should I change? 我应该改变什么? And how can I convert a KnownFolders directory to a Uri? 以及如何将KnownFolders目录转换为Uri?

The Background Audio sample uses MediaSource.CreateFromUri method to create media source. 背景音频示例使用MediaSource.CreateFromUri方法创建媒体源。 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. 使用此方法时,只能将参数设置为应用程序随附文件的统一资源标识符(URI)或网络上文件的URI。 To set the source to a file retrieved from the local system by using a FileOpenPicker object, we can use MediaSource.CreateFromStorageFile method. 要将源设置为使用FileOpenPicker对象从本地系统检索的文件,我们可以使用MediaSource.CreateFromStorageFile方法。 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. 并且只要我们的应用程序通过选择器访问文件或文件夹,我们就可以将其添加到应用程序的FutureAccessListMostRecentlyUsedList中以进行跟踪。

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: 例如,从FileOpenPicker获取StorageFile之后,我们可以将其添加到FutureAccessList并存储令牌,应用程序以后可以使用该令牌在应用程序的本地设置中检索存储项,例如:

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 . 有关FutureAccessList更多信息,请参阅跟踪最近使用的文件和文件夹

Then in BackgroundAudioTask , I changed CreatePlaybackList method to replace the original like: 然后在BackgroundAudioTask ,我更改了CreatePlaybackList方法来替换原始方法,例如:

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. 这只是一个简单的示例,您可能需要更改SongModel和其他一些代码才能实现自己的播放器。 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. 此外,在Windows 10版本1607中,对媒体播放API进行了重大改进,包括用于背景音频的简化的单进程设计。 You can see Play media in the background to check the new feature. 您可以在后台看到播放媒体来检查新功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM