UWP MediaPlayerElement页面导航返回时频繁出现未知错误求助
I’ve run into similar flaky behavior with MediaPlayerElement after navigation in UWP apps, so here are some targeted troubleshooting steps and fixes based on your scenario:
Fix MediaPlayer lifecycle management
TheMediaPlayerunderlying theMediaPlayerElementholds onto system resources that don’t always clean up automatically when navigating away. If these resources are left in a limbo state, returning to the page can cause conflicts. Try adding cleanup logic in your page’sOnNavigatedFromevent, then reinitialize the source on return:protected override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); if (MediaPlayerElement.MediaPlayer != null) { MediaPlayerElement.MediaPlayer.Pause(); MediaPlayerElement.MediaPlayer.Dispose(); MediaPlayerElement.Source = null; } } protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); // Recreate MediaSource here instead of reusing the old instance MediaSource = MediaSource.CreateFromStorageFile(yourStorageFile); }Secure StorageFile access permissions
UWP’s file access permissions can be temporary (especially for files picked via the file picker). If the token for yourStorageFileexpires after navigation, theMediaSourcecan’t access the file anymore. UseFutureAccessListto preserve access:// When you first get the StorageFile (e.g., from FilePicker) string fileToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(yourStorageFile); // Save this token somewhere persistent (like app settings) // On page navigation back var restoredFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(savedFileToken); MediaSource = MediaSource.CreateFromStorageFile(restoredFile);Capture detailed error info
The generic "unknown error" message isn’t helpful. Hook up theMediaFailedevent on theMediaPlayerto get specific error details—this will let you pinpoint whether it’s a file access issue, codec problem, or resource conflict:// Initialize this when setting up the MediaPlayerElement MediaPlayerElement.MediaPlayer.MediaFailed += (sender, errorArgs) => { Debug.WriteLine($"Media Failure: {errorArgs.Error.Message} | HResult: {errorArgs.Error.HResult}"); // Log this info for debugging };Delay MediaSource initialization
Sometimes the UI isn’t fully loaded when you set theMediaSourceright after navigation, leading to race conditions. Use the page’sLoadedevent or dispatch the initialization to the UI thread with a slight delay:protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { // Recreate your StorageFile and MediaSource here var restoredFile = await GetYourStorageFile(); MediaSource = MediaSource.CreateFromStorageFile(restoredFile); }); }Avoid reusing MediaSource instances
You mentioned you only set theMediaSourceonce, butMediaSourceis tied to the lifecycle of the page and its underlying resources. If the page is unloaded (and possibly garbage collected) during navigation, the oldMediaSourceinstance becomes invalid. Always create a newMediaSourcewhen navigating back to the page instead of reusing the original one.
内容的提问来源于stack exchange,提问作者DevNoob

