Windows UWP应用中的MediaElement无法播放音频

时间:2016-09-02 13:07:05

标签: c# windows audio uwp media

        MediaElement player = new MediaElement();
        player.Source = new Uri("ms-appx:///Assets/Media/backgroundtrack.mp3");
        player.Volume = 100;
        player.Play();

这段代码有什么问题?当我运行程序时,没有声音。

1 个答案:

答案 0 :(得分:0)

var folder = await StorageFolder.GetFolderFromPathAsync("ms-appx:///Assets/Media");
if(folder != null)
{
    var file = await folder.GetFileAsync("backgroundtrack.mp3");
    if(file != null)
    {
        var stream = await file.OpenReadAsync();
        mediaElement.SetSource(stream, file.ContentType);
        player.Volume = 100;
        player.Play();
    }
}