【问题标题】:Use mediaelement to randomly play files in a folder chosen through folderpicker使用 mediaelement 随机播放通过文件夹选择器选择的文件夹中的文件
【发布时间】:2015-08-22 17:23:19
【问题描述】:
我是 WPF 和 Windows 通用应用程序的新手。我有一个旧的桌面应用程序,我想将它重新创建为通用应用程序。它将允许用户指定一个文件夹(通过文件夹选择器),然后 mediaElement 将播放该文件夹中的随机文件。当通过文件选择器选择时,我让播放器播放文件,但到目前为止,我还无法弄清楚如何播放通过文件夹选择器选择的文件夹中的任何文件。当我让他们选择授予权限的文件夹,然后尝试将源设置为该文件夹中文件的绝对 uri 时,我仍然收到以下错误“MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED:HRESULT - 0x80070005”。任何帮助表示赞赏!谢谢!
【问题讨论】:
标签:
c#
xaml
win-universal-app
【解决方案1】:
当我们尝试在您通过文件夹选择器选择的文件夹中播放媒体文件时,我们需要使用 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();
});
}
}
}