C# 如何使用文件URL机制在Metro中播放音乐
我需要使用文件URL播放音乐库文件,我将在XAML c#对象中设置为MediaPlayer对象 我构建了如下URIC# 如何使用文件URL机制在Metro中播放音乐,c#,xaml,media,windows-8,microsoft-metro,C#,Xaml,Media,Windows 8,Microsoft Metro,我需要使用文件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 -火车发出孤独的声音 我需要基于URL的方案
StorageFile file = await KnownFolders.MusicLibrary.GetFileAsync(track.Id);
return new Uri("file:///" + file.Path);
URI如下所示:streamingUri={file:///C:/Users/user/Music/04 -火车发出孤独的声音
我需要基于URL的方案来播放,这样我也可以在web流媒体中重用相同的逻辑
我该如何使其工作?看一看
. 它应该会让您了解如何从文件中播放媒体
虽然我注意到你说你需要一个基于URI的文件,但是你应该为本地文件使用一个流。您需要提取的唯一部分是调用来设置MediaElement的源。您只需创建一个具有2个覆盖的函数,它应该相对干净
因此,对于web流:
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);
}