Search code examples
c#uwpmedia-player

UWP Unsupported Video Type - which event to catch?


I'm writing an UWP application that has a video player and I'm using the standard UWP Media Player (membername is a string obtained from an input dialog):

XAML:

     <MediaPlayerElement x:Name="mediaPlayerElement" 
                        Grid.ColumnSpan="3" 
                        Grid.RowSpan="4" 
                        AreTransportControlsEnabled="True" 
                        RequestedTheme="Dark" 
                        HorizontalAlignment="Stretch" 
                        VerticalAlignment="Stretch"

         <MediaPlayerElement.TransportControls >

            <local:CustomMediaTransportControls IsCompact="False"
                                                IsZoomButtonVisible="True"
                                                IsZoomEnabled="True"
                                                IsPlaybackRateButtonVisible="True"
                                                IsPlaybackRateEnabled="True"
                                                Opacity="0.5"


            </local:CustomMediaTransportControls>
        </MediaPlayerElement.TransportControls>
    </MediaPlayerElement>

And c# code (membername is a string obtained from somewhere else in the app and it is a valid path, checked this one many times. It is obtained from a picker.):

     StorageFile myvid = await StorageFile.GetFileFromPathAsync(membername)
     mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(myvid);
     mediaPlayerElement.MediaPlayer.Play();

For some files, like ".flv", I get an error that appears on the screen: "Unsupported Video Type". I'm trying to catch this error in order to use an MPEG decoder instead of the standard one. I know, because I tested it, that the MPEG decoder will do the job.

However, I don't know which event to catch. I've been fiddling with trying to look at the value of MediaPlaybackSession.MediaPLaybackState and it's always equal to 0..?!? I've also been trying to catch MediaPlayer.MediaFailed event, and it does not seem to get generated for this error. I'm not sure where to look. Any ideas?


Solution

  • You could handle the MediaPlayer.MediaFailed Event directly like the following:

    StorageFile myvid = await StorageFile.GetFileFromPathAsync(membername);
    mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(myvid);
    mediaPlayerElement.MediaPlayer.MediaFailed += MediaPlayer_MediaFailed;
    mediaPlayerElement.MediaPlayer.Play();
    
    private void MediaPlayer_MediaFailed(Windows.Media.Playback.MediaPlayer sender, Windows.Media.Playback.MediaPlayerFailedEventArgs args)
    {
        Debug.WriteLine(args.Error);
    }