为什么 MediaElement 有时会默默地失败,我该如何纠正? [英] Why does MediaElement sometimes silently fail and how can I correct it?

查看:23
本文介绍了为什么 MediaElement 有时会默默地失败,我该如何纠正?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my WPF project I have created a view that includes several MediaElements playing videos. Sometimes, anywhere between one or all of the MediaElements will fail to play the video assigned to them, and will instead show a black rectangle, or not appear at all. The MediaFailed event does not occur when this happens. The MediaOpened event DOES get raised on all the MediaElements, even when they do not play the video.

我已经为我的显卡安装了最新的驱动程序,但对这个问题没有影响.

I have installed the latest drivers for my graphics card and that has had no effect on this issue.

是否可以使用某种程序来确保每个 MediaElement 始终播放?

Is there a procedure I can use to ensure that every MediaElement consistently plays?

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

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

主窗口.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

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

推荐答案

MediaElement 使用 EVR,它反过来使用 Direct3D.EVR 是一种有限的资源,您为每个 MediaElement 使用一个实例,有时播放会达到限制.不幸的是,该问题在 API 内部某处被抑制,并且没有发生合理的错误报告.

MediaElement uses EVR, which in turn uses Direct3D. EVR is a limited resource, which you are using an instance per MediaElement, and once in a while the playback hits the limit. Unfortunately, the problem is suppressed somewhere inside the API and no reasonable error reporting takes place.

您可以使用此答案中引用的工具估计播放实例的数量:使用多个实例 VMR 9 的黑色视频(那里也有 EVR 选项).

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

这篇关于为什么 MediaElement 有时会默默地失败,我该如何纠正?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆