Why does MediaElement sometimes fail and how can I fix it?

In my WPF project, I created a view that includes several MediaElements that play videos. Sometimes, somewhere between one or all MediaElements, the video assigned to them will not play and will instead display a black rectangle or not appear at all. The MediaFailed event does not occur when this occurs. The MediaOpened event appears on all media elements, even if they do not play video.

I installed the latest drivers for my video card, and this did not affect this problem.

Is there a procedure that I can use to ensure continuous playback of each MediaElement?

Sample source code below. A complete project example, including video files, is on github at https://github.com/duggulous/MediaElementIssueExample

MainWindow.xaml

<Window x:Class="VideoDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="1920" Width="1080"> <Grid Name="rootContainer" Background="Pink"> <Grid.ColumnDefinitions> <ColumnDefinition Width="404" /> <ColumnDefinition Width="136" /> <ColumnDefinition Width="136" /> <ColumnDefinition Width="134" /> <ColumnDefinition Width="270" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="270" /> <RowDefinition Height="30" /> <RowDefinition Height="412" /> <RowDefinition Height="416"/> <RowDefinition Height="526"/> <RowDefinition Height="264"/> </Grid.RowDefinitions> <MediaElement x:Name="panel1" Margin="-1" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2" Source="media/1-ekg.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel2" Margin="-1" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="3" Grid.RowSpan="1" Source="media/2-star.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel3" Margin="-1" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Grid.RowSpan="1" Source="media/3-verticalLines.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel4" Margin="-1" Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2" Grid.RowSpan="2" Source="media/4-horizLines.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel5" Margin="-1" Grid.Column="4" Grid.Row="1" Grid.ColumnSpan="1" Grid.RowSpan="2" Source="media/5-curvedLines.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel6" Margin="-1" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="1" Grid.RowSpan="1" Source="media/6-glassCircles.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel7" Margin="-1" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="2" Grid.RowSpan="1" Source="media/7-diagZigZag.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel8" Margin="-1" Grid.Column="3" Grid.Row="3" Grid.ColumnSpan="2" Grid.RowSpan="1" Source="media/8-glassBurst.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel9" Margin="-1" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="2" Source="media/9-wavyLines.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel10" Margin="-1" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="4" Grid.RowSpan="1" Source="media/10-circle.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> <MediaElement x:Name="panel11" Margin="-1" Grid.Column="1" Grid.Row="5" Grid.ColumnSpan="4" Grid.RowSpan="1" Source="media/11-wavyShape.mp4" MediaOpened="panel1_MediaOpened" MediaFailed="panel1_MediaFailed" MediaEnded="panel1_MediaEnded"/> </Grid> 

MainWindow.xaml.cs

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace VideoDemo { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void panel1_MediaOpened(object sender, RoutedEventArgs e) { Console.WriteLine("Opened: "+((MediaElement)sender).Source); } private void panel1_MediaFailed(object sender, ExceptionRoutedEventArgs e) { Console.WriteLine("Failed:"+((MediaElement)sender).Source); } private void panel1_MediaEnded(object sender, RoutedEventArgs e) { Console.WriteLine("Ended:"+((MediaElement)sender).Source); } } } 
+5
source share
1 answer

MediaElement uses EVR , which in turn uses Direct3D. EVR is a limited resource in which you use an instance for MediaElement, and from time to time playback reaches its limit. Unfortunately, the problem is suppressed somewhere inside the API and no reasonable error messages occur.

You can estimate the number of playback instances using the tool referenced by this answer: Black video using multiple instances of VMR 9 (there is an EVR option there).

+4
source

Source: https://habr.com/ru/post/1200048/


All Articles