在UWP应用程序中,MediaElement用于播放视频、音频和图片。 对于视频和音频播放来说,它效果很好。但对于图片来说,就有些问题了。 代码如下。当我选择一张图片时,例如png 文件,然后您可以看到 MediaElement 上显示的错误信息
video
。
错误:不支持的视频类型或无效的文件路径
private async void loadButton_Click(object sender, RoutedEventArgs e)
{
//Create a new picker
FileOpenPicker filePicker = new FileOpenPicker();
//Add filetype filters. In this case wmv and mp4.
filePicker.FileTypeFilter.Add(".wmv");
filePicker.FileTypeFilter.Add(".mp4");
filePicker.FileTypeFilter.Add(".mp3");
filePicker.FileTypeFilter.Add(".png");
//Set picker start location to the video library
filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
//Retrieve file from picker
StorageFile file = await filePicker.PickSingleFileAsync();
//If we got a file, load it into the media element
if (file != null)
{
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
video.SetSource(stream, file.ContentType);
}
}
源码来自 https://learn.microsoft.com/en-us/samples/microsoft/windows-universal-samples/basicmediacasting/ 我的最终目标是通过DLNA将视频、音频和图片投射到远程智能电视,但这里被阻止了。
MediaElement
仅支持可流式传输的格式,因此视频和音频格式可以使用,但图像不行。
要使其工作,您必须将图像转换为视频。
private async void loadButton_Click(object sender, RoutedEventArgs e)
{
//Create a new picker
FileOpenPicker filePicker = new FileOpenPicker();
//Add filetype filters. In this case wmv and mp4.
filePicker.FileTypeFilter.Add(".wmv");
filePicker.FileTypeFilter.Add(".mp4");
filePicker.FileTypeFilter.Add(".mp3");
filePicker.FileTypeFilter.Add(".png");
//Set picker start location to the video library
filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
//Retrieve file from picker
StorageFile file = await filePicker.PickSingleFileAsync();
if (file == null)
{
//Show error message
return;
}
//If we got an image, convert it to video
if (file.ContentType.StartsWith("image/"))
{
//TODO: Create video from the image and save it to temp location
//TODO: Replace current file variable with newly created video file
if(file == null)
{
//Show error message
return;
}
}
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
video.SetSource(stream, file.ContentType);
}