Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用文件URL机制在Metro中播放音乐_C#_Xaml_Media_Windows 8_Microsoft Metro - Fatal编程技术网

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

C# 如何使用文件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的方案

我需要使用文件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的方案来播放,这样我也可以在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); 
}