UWP。如何打开多个视频

UWP. How to open multiple videos

如何播放一个文件夹中的所有文件。此功能无法正常工作:

    FolderPicker folderPicker = new FolderPicker();
    folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    folderPicker.FileTypeFilter.Add("*");
    folder = await folderPicker.PickSingleFolderAsync();
    if (folder != null)
    {
        fileList = await folder.GetFilesAsync();
        foreach (StorageFile file in fileList)
        {
            mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
            mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
            mediaPlayerElement.MediaPlayer.Play(); // <-?? (only the last file in folder)
        }
    }

我找到了解决方案。

    int ind;
    private StorageFolder folder = null;
    private IReadOnlyList<StorageFile> fileList = null;

    public MainPage()
    {
        InitializeComponent();

        mediaPlayerElement.MediaPlayer.MediaEnded += MediaPlayer_VideoFrameEndedAsync;
    }


    private async void MediaPlayer_VideoFrameEndedAsync(object sender, object args)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            ind++;
            if (ind < fileList.Count)
            {
                mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(fileList[ind]);
                mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
                mediaPlayerElement.MediaPlayer.Play();
                notify.Text = fileList[ind].DisplayName;
            }
        });
    }

    private async void folder_ClickAsync(object sender, RoutedEventArgs e)
    {
        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        folderPicker.FileTypeFilter.Add("*");
        folder = await folderPicker.PickSingleFolderAsync();
        if (folder != null)
        {
            fileList = await folder.GetFilesAsync();
            if (fileList.Count > 0)
            {
                ind = 0;
                mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(fileList[ind]);
                mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
                mediaPlayerElement.MediaPlayer.Play(); // <-?? (only the last file in folder)
                notify.Text = fileList[ind].DisplayName;
            }

            //foreach (StorageFile file in fileList)
            //{
            //    mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
            //    mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
            //    mediaPlayerElement.MediaPlayer.Play(); // <-?? (only the last file in folder)

            //    notify.Text = file.DisplayName;
            //}
        }
        else
        {
            notify.Text = "Operation cancelled.";
        }
    }