在Windows手机上播放单个音频文件

时间:2014-06-11 21:33:29

标签: windows-phone-8 windows-phone windows-phone-8.1

我使用此代码播放单个mp3音频文件,我使用了MediaElement:

Uri Path = new Uri("AudioFiles/music.mp3", UriKind.Relative);
SoundPlayer.Source = Path;
SoundPlayer.Play();

但我收到了这个错误:

  
    

类型' System.ArgumentException'的例外情况发生在mscorlib.ni.dll但未在用户代码中处理

         

其他信息:无法将给定的System.Uri转换为Windows.Foundation.Uri。

  

这有什么问题?

由于

2 个答案:

答案 0 :(得分:1)

Uri Path = new Uri("/AudioFiles/music.mp3", UriKind.Relative);

你忘记了" /"在AudioFiles之前。

答案 1 :(得分:1)

根据文件的位置,您可能需要不同的Uri scheme。尝试:

SoundPlayer.Source = new Uri(@"ms-appdata:///local/AudioFiles/music.mp3"); // if your file is in IsolatedStorage
SoundPlayer.Source = new Uri(@"ms-appx:///AudioFiles/music.mp3"); // if your file is a Build-in content

另请注意,如果SoundPlayer.Play()设置为 true (默认设置),则不需要SoundPlayer.Autoplay。如果它是 false - 您必须在MediaOpened事件中手动开始播放,而不仅仅是在设置Source之后。