How do you make Speech to Text work in Windows (Ph

2019-08-31 05:55发布

问题:

I'm trying to write code to read aloud an incoming Toast (this was trivial in WP8.1) I have this so far

  • Using MediaElement doesn't seem to work (code runs but no audio) either on the phone or in the emulator
  • Using BackgroundMediaPlayer works in the emulator but not on the phone

I've tried both from the UI thread (MediaElement only works on the UI thread) and BackgroundMediaPlayer from the thread that handles the incoming toast

var mediaElement = new MediaElement();
using (var tts = new SpeechSynthesizer())
{
    using (var ttsStream = await tts.SynthesizeSsmlToStreamAsync(ssml))
    {
        //BackgroundMediaPlayer.Current.SetStreamSource(ttsStream);
        mediaElement.SetSource(ttsStream, ttsStream.ContentType);
        mediaElement.Play();
    }
}

I'm obviously missing something simple here but I'm out of ideas how to make this work. The SSML is correct, I think it's probably something to do with scoping and threads

回答1:

     var synth = new SpeechSynthesizer();
     var voice = SpeechSynthesizer.DefaultVoice;
     var newuserText = TheMessage
     var stream = await synth.SynthesizeTextToStreamAsync(newuserText);
     var mediaElement = new MediaElement();
     mediaElement.SetSource(stream, stream.ContentType);
     mediaElement.Play();