Today, we encountered quite an interesting problem. MediaElement
wasn't firing events. Using simple speech synthesis on Windows 8.1 using the speech synthesiser we tried:
1 2 3 4 5 6 7 8 9 |
using(SpeechSynthesizer synthesizer = new SpeechSynthesizer()) { SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync("How are you feeling today?"); MediaElement mediaElement = new MediaElement(); mediaElement.IsMuted = false; mediaElement.SetSource(stream, stream.ContentType); mediaElement.MediaEnded += (a, b) => { System.Diagnostics.Debug.WriteLine("Steam has eneded."); }; mediaElement.Play(); } |
None of the variations worked. The speech worked just the event wasn't getting fired. After a bit of digging it turned out that in order to fire events MediaElement must be a part of the XAML (or the UI)
1 2 3 4 |
<Grid> <Button Click="TestButton_Click" HorizontalAlignment="Center" VerticalAlignment="Center">Test</Button> <MediaElement x:Name="mediaElement"></MediaElement> </Grid> |
1 2 3 4 5 6 7 8 9 |
using(SpeechSynthesizer synthesizer = new SpeechSynthesizer()) { SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync("How are you feeling today?"); MediaElement mediaElement = this.mediaElement; mediaElement.IsMuted = false; mediaElement.SetSource(stream, stream.ContentType); mediaElement.MediaEnded += (a, b) => { System.Diagnostics.Debug.WriteLine("Steam has eneded."); }; mediaElement.Play(); } |