windows-phone-8.1mediaelementdurationspeechsynthesizer

How to get duration of speech synthesizer stream


How can I measure duration of SpeechSynthesierStream before playing it please?

     SpeechSynthesizer ss = new SpeechSynthesizer();
     SpeechSynthesisStream sss;
     ss.Voice = SpeechSynthesizer.AllVoices[3];
     sss = await ss.SynthesizeTextToStreamAsync("Hello world.");
     MediaElement mediaElement = new MediaElement();
     mediaElement.SetSource(sss, sss.ContentType);

     //here I want to get the duration

     mediaElement.Play();

This code works just fine and speaks "Hello world.". After some time the mediaElement.NaturalDuration has also correct value. But I need to know duration of speech as soon as possible and before playing the speech. I added handler for mediaElement.MediaOpen, but this "SetSource" probably doesn't fire this event. Thank you.


Solution

  • In order for MediaOpen to fire, you need to attach the MediaElement to the visual tree. Once you've attached the MediaElement to the visual tree, NaturalDuration should be available within the MediaOpen event.