Share via


SpeechSynthesisConnector

A SpeechSynthesisConnector instance provides a Stream interface to feed audio data to an attached AudioVideoFlow instance. The Stream can be given to a SpeechSynthesizer object (in the Microsoft.Speech.Synthesis namespace) as an output destination, in order to play text-to-speech (TTS) over the wire.

Before an application can call the Start method on a SpeechSynthesisConnector instance, an AudioVideoFlow instance must have been previously attached to the connector, and the State of the AudioVideoFlow instance must be Active. If both conditions are not met, Start throws an exception.

Example

The following procedure describes the steps involved in creating and using a SpeechSynthesisConnector instance.

To use SpeechSynthesisConnector

  1. The application uses the constructor to create a SpeechSynthesisConnector instance.

    SpeechSynthesisConnector synthConnector = new SpeechSynthesisConnector();
    
  2. The application then calls AttachFlow to bind the SpeechSynthesisConnector instance to an AudioVideoFlow instance. In this code example, it is assumed that AVFlow has already been constructed, and is in the Active state.

    synthConnector.AttachFlow(AVFlow);
    
  3. The application then calls SetOutputToAudioStream on the SpeechSynthesizer instance (assumed to have been previously constructed) to identify the stream to use as well as how the audio data will be formatted.

    synthesizer.SetOutputToAudioStream(synthConnector.Stream, AudioFormat);
    
  4. The application then calls Start on a SpeechSynthesisConnector instance, causing this device to start.

    synthConnector.Start()
    
  5. The application can then use Speak to utter a prompt.

    synthesizer.Speak(prompt);
    
  6. When the Speak operation is complete or after the SpeakCompleted event is raised, the application uses Stop, to stop the SpeechSynthesisConnector instance.

    synthConnector.Stop();
    

    If another prompt is to be played, call Start again to restart the SpeechSynthesisConnector, and then call Speak again.