gpt4 book ai didi

c# - 如何使用文件 URL 机制在 Metro 中播放音乐

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:54 26 4
gpt4 key购买 nike

我需要使用文件 URL 播放音乐库文件,我将在 XAML c# 对象中将其设置为 MediaPlayer 对象。

我按照下面的方式构建了 URI

StorageFile file = await KnownFolders.MusicLibrary.GetFileAsync(track.Id);

return new Uri("file:///" + file.Path);

URI 如下所示:streamingUri = {file:///C:/Users/user/Music/04 - A Train Makes A Lonely Sound.mp3}

我需要基于 URL 的方案来播放,这样我也可以为网络流媒体重用相同的逻辑。

我如何使它工作?

最佳答案

看看 this sample .它应该让您了解如何从文件进行媒体播放。

虽然我注意到您说您需要基于 URI 的文件,但您应该为本地文件使用流。您需要提取的唯一部分是设置 MediaElement 的源的调用。您可以只创建一个具有 2 个覆盖的函数,它应该相对干净。

因此,对于网络流:

void SetMediaElementSource(Uri webStreamUri)
{
MyMediaElement.Source = webStreamUri;
}

对于本地文件:

void SetMediaElementSource(StorageFile file)
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
MyMediaElement.SetSource(stream, file.ContentType);
}

关于c# - 如何使用文件 URL 机制在 Metro 中播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10097989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com