【问题标题】:WPF MediaElement: Video opened twiceWPF MediaElement:视频打开了两次
【发布时间】:2019-05-14 23:36:25
【问题描述】:

我想将 MediaPlayer 用于带有 MediaElement 的视频和图片。我已经做过测试,MediaElement也可以显示图片。 目前我的问题是 MediaElement 似乎打开了两次。 下面是示例代码:

XAML:

<Window x:Class="TestMediaElement.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestMediaElement"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid Margin="10">
    <MediaElement Name="mediaPlayer" MediaOpened="media_MediaOpened" LoadedBehavior="Play" UnloadedBehavior="Manual"/>
</Grid>
</Window>

C#

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 TestMediaElement
{

public partial class MainWindow : Window
{
        int currentMediaIndex = 0;
        string[] Documents;

        public MainWindow()
        {
            InitializeComponent();

            Documents = System.IO.Directory.GetFiles("C:/Users/Feller/Desktop/Test/");

            Uri first = new Uri(Documents[0], UriKind.RelativeOrAbsolute);

            mediaPlayer.Source = first;

            mediaPlayer.MediaOpened += media_MediaOpened;       

        }

        private void media_MediaOpened(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Video opened");
        }

    }
}

另一个问题是图像会在大约 5 秒后自动关闭。 谁能帮我解决这些问题? 非常感谢!

【问题讨论】:

  • 媒体元素不会打开两次,图像也可见且不会关闭。您可能缺少任何代码。
  • MediaOpened Event中的Console.Write函数直接执行了两次。我不明白为什么我会收到两次此消息。

标签: c# wpf xaml mediaelement


【解决方案1】:

MediaOpened 事件被订阅了两次。从 xaml 中删除或从代码中删除。

    //mediaPlayer.MediaOpened += media_MediaOpened; 

【讨论】:

    猜你喜欢
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2015-07-03
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多