I am working on a WPF desktop application, Windows 10, Visual Studio 2015.
In which when I go to the location of the video it plays, 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 when giving the video path it 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 path but nothing is played in the MediaElement control.
I don't know what could be happening, it doesn't play anything? Do I need any extra configuration to the control or the form? I hope you can help me in advance thanks.
NOTE: I have carried out simple tests in which only the XAML is programmed and I have not been able to reproduce anything, that is why I think that some configuration must be missing both in the control or in the window (form). I can only hear the audio of the video but it does not show the images.
I have had too many problems with the MediaElement control and I have managed to solve it with Vlc.Dotnet that Sergio Parra recommended to me .
The implementation is as follows:
Then add the namespace to the XAML and add the control.
Add the following code to the .cs file
The MediaElement component uses the Windows codec mechanism (read about DirectShow and the Windows Media Foundation for more details on how it works internally). Basically every 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 you to process all of these formats are commonly called "codecs" (encoder/decoder or compressor/decompressor).
Windows comes with support for many container and audio/video formats by default, but there are formats that are not supported by default and require additional software to be installed.
For your particular case, try the video you are trying to play with a free "self-contained" player, videolan player for example, which does not use Windows codecs, and if it works, your video needs a codec (decoder) special that is not installed by default.
To better diagnose the problem of your application, test it with a video that you know in advance that works well on your system, using for example the Windows Media Player that comes by default.