WPF MediaElement:视频打开了两次

时间:2018-12-13 17:26:26

标签: c# wpf xaml mediaelement

我想将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秒钟后自动关闭。 谁能帮助我解决这些问题? 非常感谢你!

1 个答案:

答案 0 :(得分:0)

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

<input type="radio" name="j1" value="A">A. Answer 1<br>
<input type="radio" name="j1" value="B">B. Answer 2<br>
<input type="radio" name="j1" value="C">C. Answer 3<br>