Play video in MediaElement WPF

4

I'm working on a WPF desktop application, Windows 10, Visual Studio 2015.

In which when going to the location of the video is played, I have the following code.

XAML:

<Window x:Class="ReconocimientoVoz.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:ReconocimientoVoz"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="28*"/>
        <ColumnDefinition Width="19*"/>
    </Grid.ColumnDefinitions>
    <Rectangle x:Name="rectColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="55,46,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
    <Label x:Name="lblColor" Content="lblColor" HorizontalAlignment="Left" Margin="144,234,0,0" VerticalAlignment="Top" Width="179" Grid.ColumnSpan="2"/>
    <Button x:Name="btnEscuchar" Content="Escuchar" HorizontalAlignment="Left" Margin="62,234,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="btnEscuchar_Click"/>
    <Label x:Name="label" Content="Color:" HorizontalAlignment="Left" Margin="98,234,0,0" VerticalAlignment="Top"/>
    <MediaElement x:Name="mediaPlayer" Grid.Column="1" HorizontalAlignment="Left" Height="161" 
                  Margin="0,35,0,0" VerticalAlignment="Top" Width="200" LoadedBehavior="Play"/>

</Grid>

In Mediaelement configure LoadedBehavior in Play so that supposedly at the moment of giving the video route can be displayed in the control.

C #

private void btnEscuchar_Click(object sender, RoutedEventArgs e)
    {
        lblColor.Content = string.Empty;
        try
        {
            escucha.SetInputToDefaultAudioDevice();
            escucha.LoadGrammar(new DictationGrammar());
            //escucha.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(lector);
            escucha.SpeechRecognized += Escucha_SpeechRecognized;
            escucha.SetInputToDefaultAudioDevice();
            escucha.RecognizeAsync(RecognizeMode.Multiple);
        }
        catch (InvalidOperationException ex)
        {
            MessageBox.Show(ex.Message, "Aviso");
        }
    }

    private void Escucha_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        foreach (var palabra in e.Result.Words)
        {
            lblColor.Content = palabra.Text;
            if (palabra.Text == "rojo")
            {
                rectColor.Fill = new SolidColorBrush(Colors.Red);
            }
            else if (palabra.Text == "azul")
            {
                rectColor.Fill = new SolidColorBrush(Colors.Blue);
            }
            else if (palabra.Text == "amarillo")
            {
                rectColor.Fill = new SolidColorBrush(Colors.Yellow);
                mediaPlayer.Source = new Uri(@"D:\PruebasCSharp\ReconocimientoVoz\Video\hola.wmv");                  
            }

            else if (palabra.Text == "hola")
            {
                mediaPlayer.Source = new Uri(@"D:\PruebasCSharp\ReconocimientoVoz\Video\hola.wmv");
                //mediaPlayer.Play();
            }
        }
    }

When the condition is met, it calls the video route but nothing is played on the MediaElement control.

I do not know what may be happening, does not reproduce anything? Do I need any extra configuration to the control or the form? I hope you can help me in advance thank you.

NOTE: I have done simple tests in which only it is programmed in the XAML and I have not been able to reproduce anything, that is why I think that some configuration must be missing in the control or in the window (form). I can only hear the audio of the video but it does not show the images.

    
asked by Pedro Ávila 04.10.2017 в 00:19
source

2 answers

3

I have had too much problems with the MediaElement control and I have managed to solve it with Vlc.Dotnet that recommended Sergio Parra .

The implementation is as follows:

  • First you have to add the WindowsFormsIntegration project as a reference
  • Then add namespace to the XAML and add the control.

    <Window x:Class="ReconocimientoVoz.WindowVLC"
        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:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"**
        xmlns:local="clr-namespace:ReconocimientoVoz"
        mc:Ignorable="d"
        Title="WindowVLC" Height="300" Width="300">
    
    <Grid >
        <vlc:VlcControl x:Name="vlcPlayer" />
    </Grid>
    

  • Add the following code to the .cs file

    public partial class WindowVLC : Window
    {
        public WindowVLC()
        {
            InitializeComponent();
            vlcPlayer.MediaPlayer.VlcLibDirectory =
                //replace this path with an appropriate one
                new DirectoryInfo(@"c:\Program Files (x86)\VideoLAN\VLC\");
            vlcPlayer.MediaPlayer.EndInit();
            vlcPlayer.MediaPlayer.Play(new Uri(@"D:\PruebasCSharp\ReconocimientoVoz\Video\hola.wmv"));
        }
    }
    
answered by 04.10.2017 / 23:06
source
1

The MediaElement component uses the Windows codec mechanism (read about DirectShow and Windows Media Foundation for more details on how it works internally). Basically each video has a "container" format (mkv, avi, etc.) that tells how each part (stream) of the video is stored. The video contains several streams (video, audio, subtitle), and each one is encoded with a certain format (mpeg, divx, h264, mp3, aac, etc.) that is independent of the general container format. The Windows components that allow processing all these formats are commonly called "codec" (encoder / decoder or compressor / decompressor)

Windows comes by default with support for many container and audio / video formats, but there are formats that are not supported by default and need additional software installation.

For your particular case, try the video you are trying to play with a free "self-content" player, videolan player for example, they do not use Windows codecs, and if it works, your video needs a special codec (decoder) that is not installed by default.

To better diagnose the problem of your application, try it with a video that you know in advance that works well in your system, using for example the Windows Media Player that comes by default.

    
answered by 04.10.2017 в 17:46