UWP MediaPlaybackList添加MediaSource工作太慢

Nik*_*lov 3 c# windows-phone win-universal-app

我有的问题MediaPlaybackList,如下所示:

playbackList = new MediaPlaybackList();
playbackList.AutoRepeatEnabled = true;
for (int i = 0; i < songs.Count();i++)
{    
    var song = songs.ElementAt(i);    
    var source = MediaSource.CreateFromStorageFile(
        await StorageFile.GetFileFromPathAsync(song.File));
    source.CustomProperties[TrackIdKey] = null;
    source.CustomProperties[TitleKey] = song.Title;
    source.CustomProperties[AlbumArtKey] = song.AlbumArtUri;
    source.CustomProperties[AuthorKey] = song.Author;
    source.CustomProperties[TrackNumber] = (uint)(i+1);
    playbackList.Items.Add(new MediaPlaybackItem(source));
}
Run Code Online (Sandbox Code Playgroud)

当我尝试将其添加MediaSource到播放列表时,需要花费太多时间。700首歌曲大约需要3分钟才能开始播放。也许还有另一种添加方法可以更快地添加MediaSourceMediaPlaybackList其中?

Ste*_*ian 5

使用IRandomAccessStreamReference。这样,它只有在到达项目时才需要加载文件,这要快得多。

但是,您将必须编写自己的抽象类。可能看起来像这样:

public class MyStreamReference : IRandomAccessStreamReference
{
    private string path;

    public MyStreamReference(string path)
    {
        this.path = path;
    }

    public IAsyncOperation<IRandomAccessStreamWithContentType> OpenReadAsync()
        => Open().AsAsyncOperation();

    // private async helper task that is necessary if you need to use await.
    private async Task<IRandomAccessStreamWithContentType> Open()
        => await (await StorageFile.GetFileFromPathAsync(path)).OpenReadAsync();
}
Run Code Online (Sandbox Code Playgroud)