(注:所有代码已被严重简化。)
问题
暂停/恢复后未设置 MediaElement 源。设置源后,CurrentState 快速更改为“已关闭”。
我正在处理 MediaFailed 事件——它不会触发。我也在处理 MediaOpened 事件,它也不会触发。
细节
我有以下方法可以更新 MediaElement 的源。只要应用程序在暂停后不尝试恢复,它就可以很好地工作。
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.
}
当应用程序暂停时,它会调用SaveState方法:
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;
}
当应用程序恢复时,它会调用LoadState方法:
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;
}
}
我很感激在这个问题上的任何帮助。如果您需要更多详细信息,请告诉我。