请验证我的代码,应用程序必须从我的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++;
}
}
}
答案 0 :(得分:3)
MediaElement.Play()
没有等待播放完成,也没有内置的异步版本。 WinRT XAML Toolkit中有一种扩展方法可以通过调用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;
}
}
}