c# - UWP MediaPlaybackList 添加 MediaSource 工作太慢

标签 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));
}

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

最佳答案

使用 IRandomAccessStreamReference。 这样它只需要在到达 Item 时加载文件,速度要快得多。

不过您必须编写自己的抽象类。 可能看起来有点像这样:

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();
}

关于c# - UWP MediaPlaybackList 添加 MediaSource 工作太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36383747/

相关文章:

c# - 从 Azure 函数针对 FTP 服务器(主动模式)运行 C# FTP 客户端 - "500 Syntax error, command unrecognized"

c# - 在 Picturebox 上显示从相机检索到的位图的实验

c# - 为什么我不能使用 Windows.Phone.Media.Capture 命名空间?

c# - 如何根据我的鼠标旋转图片框

c# - 如何防止TextBlock获得自动焦点

c# - 具有任意列和行的 Wpf DataGrid 数据绑定(bind)

c# - 尝试在窗口服务中托管 .NET Core 2.1 Web 应用程序会创建 dll 而不是 exe

c# - 如何以编程方式从 Windows Phone 访问 SMS 消息线程

c# - Entity Framework 已安装,但无法访问

c# - 将 Dropbox 生成的访问 token 与 DropNetRT 结合使用