首页 文章

使用mediaelement随机播放通过folderpicker选择的文件夹中的文件

提问于
浏览
0

我是WPF和Windows Universal Apps的新手 . 我有一个旧的桌面应用程序,我想重新创建为通用应用程序 . 它将允许用户指定文件夹(通过文件夹选择器),然后mediaElement将播放该文件夹中的随机文件 . 当通过filepicker选择时,我让播放器播放文件,但到目前为止,我还没有弄清楚如何播放通过folderpicker选择的文件夹中的任何文件 . 当我让他们选择文件夹来授予它权限,然后尝试将源设置为该文件夹中文件的绝对uri我仍然收到以下错误“MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED:HRESULT - 0x80070005” . 任何帮助表示赞赏!谢谢!

1 回答

  • 0

    当我们尝试在文件夹选择器选择的文件夹中播放媒体文件时,我们需要使用StorageFile.OpenAsync打开该文件 . 并将打开的流设置为MediaElement,如下所示:

    private async void button_Click(object sender, RoutedEventArgs e)
        {
            FolderPicker folderPicker = new FolderPicker();
            folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            folderPicker.FileTypeFilter.Add(".mp3");
    
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            if (folder != null)
            {
                //get the file list
                var files = await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false);
    
                //For example, I can use the following code to play the first item
                if (files.Count > 0)
                {
                    var stream = await files[0].OpenAsync(FileAccessMode.Read);
    
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        mediaElement.SetSource(stream, files[0].ContentType);
                        mediaElement.Play();
                    });
                }
            }
        }
    

相关问题