想要打开多个音频文件并在UWP中播放

时间:2018-09-12 11:19:59

标签: c# uwp

我想使用FileOpenPicker在UWP中打开多个音频文件,但出现一个我无法转换的错误。我怎样才能解决这个问题? 如果固定的话,所有音频文件将按顺序播放还是全部同时播放?

public MainPage()
{
    this.InitializeComponent();
}
MediaSource media_source;
MediaPlayer media_player;


public async System.Threading.Tasks.Task OpenfileAsync()
{
    var filePicker = new Windows.Storage.Pickers.FileOpenPicker();

    filePicker.FileTypeFilter.Add(".mp3");
    filePicker.FileTypeFilter.Add(".mp4");
    filePicker.FileTypeFilter.Add(".ogg");
    filePicker.FileTypeFilter.Add(".wav");
    filePicker.FileTypeFilter.Add(".wma");

    filePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;

    StorageFile file = await filePicker.PickSingleFileAsync();

    if (file != null)
    {
        media_source = MediaSource.CreateFromStorageFile(file);
        media_player = new MediaPlayer();
        media_player.Source = media_source;
        mediaPlayerElement.SetMediaPlayer(media_player);
        media_player.Play();
    }
}

private async void Select_track_Click(object sender, RoutedEventArgs e)
{
    await OpenfileAsync();
}
public async System.Threading.Tasks.Task OpenMultipleAsync()
{
    var filePicker = new Windows.Storage.Pickers.FileOpenPicker();
    filePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;

    filePicker.FileTypeFilter.Add(".mp3");
    filePicker.FileTypeFilter.Add(".mp4");
    filePicker.FileTypeFilter.Add(".ogg");
    filePicker.FileTypeFilter.Add(".wav");
    filePicker.FileTypeFilter.Add(".wma");
    filePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;

    StorageFile file = await filePicker.PickMultipleFilesAsync();

    if (file != null)
    {

        media_source = MediaSource.CreateFromStorageFile(file);
        media_player = new MediaPlayer();
        media_player.Source = media_source;
        mediaPlayerElement.SetMediaPlayer(media_player);
        media_player.Play();

    }

}
private async void playlist_Click(object sender, RoutedEventArgs e)
{
    await OpenMultipleAsync();
}

我在StorageFile file = await filepicker.PickmultiplefilesAsync();遇到错误

2 个答案:

答案 0 :(得分:1)

用于播放您最好使用的文件列表MediaPlaybackList

此外,您只需要设置一次 SuggestedStartLocation ,当您使用 PickMultipleFilesAsync()时,您将获得一个返回文件列表,因此您需要遍历该列表以获取所有文件并将它们添加到您的 MediaPlaybackList

  

像这样修改 OpenMultipleAsync 方法:

public async System.Threading.Tasks.Task OpenMultipleAsync()
{
    var filePicker = new Windows.Storage.Pickers.FileOpenPicker();

    filePicker.FileTypeFilter.Add(".mp3");
    filePicker.FileTypeFilter.Add(".mp4");
    filePicker.FileTypeFilter.Add(".ogg");
    filePicker.FileTypeFilter.Add(".wav");
    filePicker.FileTypeFilter.Add(".wma");
    filePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;

    _mediaPlaybackList = new MediaPlaybackList();

    var files = await filePicker.PickMultipleFilesAsync();

    foreach (var file in files)
    {
        var mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(file));
        _mediaPlaybackList.Items.Add(mediaPlaybackItem);
    }

    _mediaPlayer = new MediaPlayer();
    _mediaPlayer.Source = _mediaPlaybackList;
    mediaPlayerElement.SetMediaPlayer(_mediaPlayer);
}

有关 MediaPlaybackItem 的更多详细信息,可以查看here

  

并回答“这些媒体文件将同时播放还是一个接一个播放”:它们将连续播放一个接一个,这就是MediaPlaybackList的目的,它支持播放列表的无缝播放。 / p>

答案 1 :(得分:0)

FileOpenPicker.PickMultipleFilesAsync方法具有以下签名:

IAsyncOperation<IReadOnlyList<StorageFile>> PickMultipleFilesAsync()

PickFileAsync相反,它返回IReadOnlyList<StorageFile>,因此您实际上将获得用户选择的多个文件的列表。您应该像这样更新代码:

var files = await filePicker.PickMultipleFilesAsync();

foreach (var file in files)
{
    if (file != null)
    {
        media_source = MediaSource.CreateFromStorageFile(file);
        media_player = new MediaPlayer();
        media_player.Source = media_source;
        mediaPlayerElement.SetMediaPlayer(media_player);
        media_player.Play();
    }
}

此解决方案将一次播放所有声音。对于一对一的播放,您可以使用@touseefbsb解决方案:-)。