【问题标题】:How can I make the SpeechSynthesizer speak through a particular device?如何让 SpeechSynthesizer 通过特定设备说话?
【发布时间】:2019-08-12 13:08:33
【问题描述】:

我是语音识别的新手,所以如果问题非常基本,请原谅。

我的应用程序通过麦克风捕捉我所说的内容。我已经使用我的 c# 代码为我说的每个命令编写了一些响应,并且 SpeechSynthesizer 使用下面提到的代码执行此操作没有任何问题。 但我希望 SpeechSynthesizer 通过我的笔记本电脑扬声器而不是我的默认输入设备(我的麦克风)做出响应。可行吗?

我目前使用的代码如下。我正在寻找可以让所有播放设备可用的东西,然后选择并使用我的扬声器回复。

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

【问题讨论】:

    标签: c# speech-recognition text-to-speech speechsynthesizer


    【解决方案1】:

    您可以使用 System.Media.SoundPlayer 类从流中输出音频。这个例子来自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.  
        }
    }
    

    我不确定 SoundPlayer 是否可以指定输出设备,但它应该使用您的默认输出设备输出。

    【讨论】:

      【解决方案2】:

      看看NAudio,它已经实现了这个功能。您可以查看他们的GIT 实现上的实现并复制您需要的代码或克隆/获取包。

      根据实现,您可以简单地循环它们

      [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));
               }
            }
      }
      

      【讨论】:

        【解决方案3】:

        基于Microsoft docs for the method - 控制面板决定了您的音频输出 - 不应该是麦克风 - 应该是带扬声器的音频设备.. 确保您已配置 - 并在控制中测试面板

        【讨论】:

          猜你喜欢
          • 2019-08-07
          • 2012-06-27
          • 1970-01-01
          • 2011-05-03
          • 1970-01-01
          • 1970-01-01
          • 2010-12-09
          • 1970-01-01
          相关资源
          最近更新 更多