为什么MediaElement有时会无声地失败,我该如何纠正呢?

时间:2014-08-11 17:31:13

标签: c# wpf video mediaelement ms-media-foundation

在我的WPF项目中,我创建了一个包含多个MediaElements播放视频的视图。有时,在一个或所有MediaElements之间的任何地方都将无法播放分配给它们的视频,而是显示黑色矩形,或根本不显示。发生这种情况时不会发生MediaFailed事件。 MediaOpened事件会在所有MediaElements上出现,即使他们不播放视频也是如此。

我已经为我的显卡安装了最新的驱动程序,这对此问题没有任何影响。

我是否可以使用一个程序来确保每个MediaElement一直播放?

示例源代码如下。包含视频文件的完整示例项目位于https://github.com/duggulous/MediaElementIssueExample

的github上

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);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

MediaElement使用EVR,后者又使用Direct3D。 EVR是一种有限的资源,您每个MediaElement都使用一个实例,并且偶尔播放达到限制。不幸的是,问题在API内部被抑制,并且没有发生合理的错误报告。

您可以使用此答案中引用的工具估算播放实例的数量:Black video using multiple instances VMR 9(此处还有EVR选项)。