如何在Windows 8中使用MediaElement连续播放视频文件

本文关键字:连续 MediaElement 播放 视频 文件 Windows | 更新日期: 2023-09-27 18:31:21

请验证我的代码,应用程序必须从我的Windows 8 PC的视频库中逐个播放视频

public sealed partial class MainPage : Page
{
    int i =0;
    public MainPage()
    {
        this.InitializeComponent();
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        StorageFolder videoLibrary = KnownFolders.VideosLibrary;
        IReadOnlyList<IStorageItem> items = await videoLibrary.GetItemsAsync();
        await LoadData(items);       
    }

    public async Task SecondCall(StorageFile x)
    {
        var y = await x.OpenAsync(FileAccessMode.Read);
        MyPlayer.SetSource(y, x.ContentType);
        MyPlayer.Play();
    }
    public async Task LoadData(IReadOnlyList<IStorageItem> itemss)
    {
        if (i <= itemss.Count)
        {
            var f = itemss[i] as StorageFile;
            await SecondCall(f);
            i++;
        }
    }
}

如何在Windows 8中使用MediaElement连续播放视频文件

MediaElement.Play()不会

等待播放完成,也没有内置的异步版本。WinRT XAML 工具包中有一个扩展方法,可以通过调用await myMediaElement.PlayToEndAsync()来使用它

如果您更喜欢,请参阅可以添加到项目中的扩展的源代码,而不是包含整个工具包库。

using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace WinRTXamlToolkit.AwaitableUI
{
    /// <summary>
    /// Extension methods for awaiting MediaElement state changes.
    /// </summary>
    public static class MediaElementExtensions
    {
        /// <summary>
        /// Waits for the MediaElement.CurrentState to change to any (default) or specific MediaElementState value.
        /// </summary>
        /// <param name="mediaElement"></param>
        /// <param name="newState">The MediaElementState value to wait for. Null by default causes the metod to wait for a change to any other state.</param>
        /// <returns></returns>
        public static async Task<MediaElement> WaitForStateAsync(this MediaElement mediaElement, MediaElementState? newState = null)
        {
            if (newState != null &&
                mediaElement.CurrentState == newState.Value)
            {
                return null;
            }
            var tcs = new TaskCompletionSource<MediaElement>();
            RoutedEventHandler reh = null;
            reh = (s, e) =>
            {
                if (newState != null && mediaElement.CurrentState != newState.Value)
                {
                    return;
                }
                mediaElement.CurrentStateChanged -= reh;
                tcs.SetResult((MediaElement)s);
            };
            mediaElement.CurrentStateChanged += reh;
            return await tcs.Task;
        }
        /// <summary>
        /// Plays to end and waits asynchronously.
        /// </summary>
        /// <param name="mediaElement">The media element.</param>
        /// <param name="source">The source to play.</param>
        /// <returns></returns>
        public static async Task<MediaElement> PlayToEndAsync(this MediaElement mediaElement, Uri source)
        {
            mediaElement.Source = source;
            return await mediaElement.WaitToCompleteAsync();
        }
        /// <summary>
        /// Waits for the MediaElement to complete playback.
        /// </summary>
        /// <param name="mediaElement">The media element.</param>
        /// <returns></returns>
        public static async Task<MediaElement> WaitToCompleteAsync(this MediaElement mediaElement)
        {
            //if (mediaElement.CurrentState != MediaElementState.Closed &&
            //    mediaElement.CurrentState != MediaElementState.Buffering &&
            //    mediaElement.CurrentState != MediaElementState.Opening &&
            //    mediaElement.CurrentState != MediaElementState.Playing)
            //{
            //    return mediaElement;
            //}
            var tcs = new TaskCompletionSource<MediaElement>();
            RoutedEventHandler reh = null;
            reh = (s, e) =>
            {
                if (mediaElement.CurrentState == MediaElementState.Buffering ||
                    mediaElement.CurrentState == MediaElementState.Opening ||
                    mediaElement.CurrentState == MediaElementState.Playing)
                {
                    return;
                }
                mediaElement.CurrentStateChanged -= reh;
                tcs.SetResult((MediaElement)s);
            };
            mediaElement.CurrentStateChanged += reh;
            return await tcs.Task;
        }
    }
}