简体   繁体   中英

How can I make the SpeechSynthesizer speak through a particular device?

I am new to the Speech Recognition, so please excuse me if the question is very basic level.

My application captures what I speak through my microphone. I have written some responses using my c# code for every command I say and the SpeechSynthesizer does this without any issues using the code mentioned below. But I want the SpeechSynthesizer to respond back through my laptop speaker, rather than my default input device (my microphone). Is it doable?

The code I am currently using is given below. I am looking for something which can get all the playback devices available and then select and speak back using my speaker.

public void SpeakTheText(string text)
{
    SpeechInput = text;
    SpeechSynthesizer _synthesizer = new SpeechSynthesizer();
    _synthesizer.SelectVoiceByHints(VoiceGender.Male);
    _synthesizer.SetOutputToDefaultAudioDevice();//Microphone
    _synthesizer.SpeakAsync(SpeechInput);                       
}

You could use the System.Media.SoundPlayer class to output the audio from a stream. See this example from MSDN

public void SpeakTheText(string text)
{  
    // Initialize a new instance of the speech synthesizer.  
    using (SpeechSynthesizer synth = new SpeechSynthesizer())  
    using (MemoryStream streamAudio = new MemoryStream())  
    {  
        // Create a SoundPlayer instance to play the output audio file.  
        System.Media.SoundPlayer m_SoundPlayer = new System.Media.SoundPlayer();  
        // Set voice to male
        synth.SelectVoiceByHints(VoiceGender.Male);
        // Configure the synthesizer to output to an audio stream.  
        synth.SetOutputToWaveStream(streamAudio);  

        // Speak a phrase.  
        synth.Speak(text);  
        streamAudio.Position = 0;  
        m_SoundPlayer.Stream = streamAudio;  
        m_SoundPlayer.Play();  

        // Set the synthesizer output to null to release the stream.   
        synth.SetOutputToNull();  

        // Insert code to persist or process the stream contents here.  
    }
}

I'm not sure if SoundPlayer can specify an output device, but it should output using your default output device.

Have a look at NAudio , it has implemented this feature. You can have a look at the implementation on their GIT implementation and copy the code you need or clone/ get the package.

as per the implementation, you can simply loop overthem

[TestFixture]
public class DirectSoundTests
{
    [Test]
    [Category("IntegrationTest")]
    public void CanEnumerateDevices()
    {
        foreach(var device in DirectSoundOut.Devices)
        {
             Debug.WriteLine(String.Format("{0} {1} {2}", device.Description, device.ModuleName, device.Guid));
         }
      }
}

基于该方法Microsoft 文档- 控制面板决定了您的音频输出 - 它不应该是麦克风 - 应该是带有扬声器的音频设备......确保你已经配置 - 并在控制面板中进行了测试

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM