Search code examples
windows-phone-8.1text-to-speechc++-cxspeech-synthesis

SpeechSynthesizer::SynthesizeTextToStreamAsync function takes forever


I have the following C++/CX code:

Windows::Media::SpeechSynthesis::SpeechSynthesizer^ synth = ref new Windows::Media::SpeechSynthesis::SpeechSynthesizer();
Platform::String^ text = "This is a string of text.";
concurrency::create_task(synth->SynthesizeTextToStreamAsync(text))
  .then([&](Windows::Media::SpeechSynthesis::SpeechSynthesisStream^ stream) {
      mediaElement->AutoPlay = true;
      mediaElement->SetSource(stream, stream->ContentType);
      mediaElement->Play();
  });

If my understanding is correct, it is supposed to synthesize the string This is a string of text. into a stream that would then be played through a MediaElement. After I execute this code, however, the lambda specified in task.then() never runs. Am I missing something?


Solution

  • This works fine for me. A few possibilities:

    1. You don't have the Microphone capability and you are swallowing an Access Denied exception(*)
    2. Calling Play is throwing and you are swallowing the exception(**)
    3. You are running on a device that doesn't have a default speech language installed (DefaultVoice == nullptr)

    (*) Although you're not actually using the Microphone for speech synthesis, the way the speech system works in Windows Phone you need this capability

    (**) You should never call Play immediately after setting the Source; you must wait for the MediaOpened event to be raised (or, in this case, rely on AutoPlay to do the work for you).