1

如何播放音乐库中的歌曲?我试过这个:

private void click_AlarmSet(object sender, RoutedEventArgs e)
{
    play();
}  

async private void play()
{
    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();

    openPicker.SuggestedStartLocation =Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
    openPicker.FileTypeFilter.Add("toxic.mp3");

    var file = await openPicker.PickSingleFileAsync();
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

    sound.SetSource(stream, file.ContentType);
    sound.Play();
}

sound是我的媒体元素,“toxic.mp3”是我想播放的 mp3 文件,但 mp3 没有播放。

4

1 回答 1

1
openPicker.FileTypeFilter.Add("toxic.mp3")

我认为 FileTypeFilter 正在寻找“.mp3”或“.wav”而不是特定的文件名。 http://msdn.microsoft.com/en-us/library/windows.storage.pickers.fileopenpicker.filetypefilter.Aspx

建议:

var name = "toxic.mp3";
var file = await openPicker.GetFileAsync(name);
var stream = await file.OpenAsync(FileAccessMode.Read);
sound.SetSource(stream, file.ContentType);

-或者- 如果你想要所有的 '.mp3'

openPicker.FileTypeFilter.Add(".mp3")
于 2014-09-13T03:07:32.477 回答