【问题标题】:MediaPlayerElement not playing videos, in addition to not taking the path from my removeable deviceMediaPlayerElement 不播放视频,除了不从我的可移动设备中获取路径
【发布时间】:2019-10-27 12:20:28
【问题描述】:

我正在尝试通过在后面的代码中设置其源来使用 MediaPlayerElement 从我的闪存驱动器播放视频。我已经验证了路径是正确的(至少我认为是正确的)并且格式如下:D:\\video.mp4。将其传递给 CreateFromUri 时,它说文件路径错误。

代码背后:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();


        GetDrive();
    }

    public async void GetDrive()
    {
        StorageFolder externalDevices = KnownFolders.RemovableDevices;
        IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
        StorageFolder x = externalDrives[0];
        IReadOnlyList<StorageFile> items = await x.GetFilesAsync();

        foreach (StorageFile file in items)
        {
            try
            {
                Uri uri = new Uri(file.Path);
                mediaPlayer.Source = MediaSource.CreateFromUri(uri);
            }
            catch (Exception ex)
            {

            }
        }
    }
}

页面:

<Grid >

    <MediaPlayerElement x:Name="mediaPlayer"
                Width="400"
                AutoPlay="True"
                AreTransportControlsEnabled="True"/>

</Grid>

【问题讨论】:

    标签: c# uwp uwp-xaml windows-10-iot-core


    【解决方案1】:

    对于 CreateFromUri 方法,您不能直接将文件路径参数传递给 Uri。如果你想通过 Uri 访问 UWP 中的资源,可以使用多个 URI (Uniform Resource Identifier) schemes 来引用来自应用程序包、应用程序数据文件夹或云的文件。为什么不使用 CreateFromStorageFile 方法呢?

        public async void GetDrive()
        {
            StorageFolder externalDevices = KnownFolders.RemovableDevices;
            IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
            StorageFolder x = externalDrives[0];
            IReadOnlyList<StorageFile> items = await x.GetFilesAsync();
    
            foreach (StorageFile file in items)
            {
                try
                {
                    mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
                }
                catch (Exception ex)
                {
    
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多