(注意:所有代码都已被严重简化。)
Problem
暂停/恢复后未设置 MediaElement 源。设置源后,CurrentState 会快速更改为“Closed”。
我正在处理 MediaFailed 事件 - 它不会触发。我还在处理 MediaOpened 事件,该事件也不会触发。
Details
我有以下方法可以更新 MediaElement 的源。只要应用程序运行,它就可以很好地工作not暂停后尝试恢复。
private async void UpdateMediaElementSource(object sender, EventArgs e)
{
var videoSource = this.DefaultViewModel.CurrentSource; // a string
var file = await StorageFile.GetFileFromPathAsync(videoSource);
var videoStream = await file.OpenAsync(FileAccessMode.Read);
this.videoMediaElement.SetSource(videoStream, file.ContentType);
// The above line works many times as long as the app is not trying to Resume.
}
当应用程序暂停时,它会调用保存状态 method:
protected async override void SaveState(Dictionary<String, Object> pageState)
{
pageState["MediaElementSource"] = this.DefaultViewModel.CurrentSource;
// I also made the videoStream global so I can dispose it — but no dice.
this.videoStream.Dispose();
this.videoStream = null;
}
当应用程序恢复时,它会调用加载状态 method:
protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
string source = string.Empty;
if (pageState != null)
{
if (pageState.ContainsKey("MediaElementSource"))
{
source = (string)pageState["MediaElementSource"];
}
}
var document = PublicationService.GetDocument(this.currentDocumentIdNumber);
this.DefaultViewModel = new DocumentViewModel(document);
this.DefaultViewModel.CurrentMarkerSourceChanged += UpdateMediaElementSource;
if (!string.IsNullOrEmpty(source))
{
// This causes the UpdateMediaElementSource() method to run.
this.DefaultViewModel.CurrentSource = source;
}
}
我很感谢有关此问题的任何帮助。如果您需要更多详细信息,请告诉我。