Question

I have an application that has a high dependency on videos and it uses MediaElement to reproduce them.

However, I have introduced a layer that either gets a video from an external folder or an internal folder. Therefore, in my internal folder, I am loading my videos like this:

private static async void 
   InitVideo(MediaElement mediaElement, string resourceName)
   {
        try {
            Uri imageUri = 
                new Uri(string.Format("ms-appx:///{0}", resourceName),
                                      UriKind.RelativeOrAbsolute);
            StorageFile file = 
                     await StorageFile.GetFileFromApplicationUriAsync(imageUri);
            var stream = 
                     await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            mediaElement.SetSource(stream, file.ContentType);

        }
        catch (System.Exception ex)
        {
            Debug.WriteLine("Error loading video ({0})", resourceName);
            Debug.WriteLine(ex.Message);
        }
    }

and then in my XAML I have something like this:

lextensions:MediaElementExtensions.MediaResource="video1.mov"

MediaResource is an attached property that calls this when it changes:

MediaElement mediaElement = sender as MediaElement;
if (mediaElement != null) {
    mediaElement.Tag = args.NewValue; // pass the new resource name.
    mediaElement.Loaded += LoadVideo;
}

When the MediaElement is loaded the LoadVideo is called and that calls InitVideo.

This works fine a lot of times. However, in rare cases, this doesn't work and I have no idea why. I am using mov files with H.264 codecs.

I have seen that some people have this issue. However, in their case they weren't able to display any video at all. And I can do that. Actually, it works in more than 80-90% of the times.

Was it helpful?

Solution

So far I didn't experienced the videos disappearing. I have updated the videocard drivers and take the considerations that @danielovich pointed out.

The moral is: Avoid async void. And this video does a good job explaining why.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top