将 Speech.Synthesizer 发送到特定设备

Send Speech.Synthesizer to a specific Device

我正在使用 Microsoft 语音合成并希望将输出重定向到我选择的输出音频设备。

到目前为止我有以下代码:

SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
speechSynthesizer.SpeakAsync("Yea it works!");

目前我正在使用:

speechSynthesizer.SetOutputToDefaultAudioDevice();

但我实际上想将它发送到我选择的设备。我正在寻找一个关于如何引导我选择的输出设备的 cscore 示例。我看到我可以使用:

speechSynthesizer.SetOutputToWaveStream();

这需要 "Stream",但我不知道如何喂它。

谢谢。

您可以创建一个 MemoryStream and attach it to CSCore's WaveOut class. WaveOut requires an IWaveSource argument, so you can use CSCore's MediaFoundationDecoder to convert the wave stream from SpeechSynthesizer。我做了一个小控制台应用程序来说明:

using System;
using System.IO;
using System.Speech.Synthesis;
using CSCore;
using CSCore.MediaFoundation;
using CSCore.SoundOut;

namespace WaveOutTest
{
    class Program
    {
        static void Main()
        {
            using (var stream = new MemoryStream())
            using (var speechEngine = new SpeechSynthesizer())
            {
                Console.WriteLine("Available devices:");
                foreach (var device in WaveOutDevice.EnumerateDevices())
                {
                    Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
                }
                Console.WriteLine("\nEnter device for speech output:");
                var deviceId = (int)char.GetNumericValue(Console.ReadKey().KeyChar);

                speechEngine.SetOutputToWaveStream(stream);
                speechEngine.Speak("Testing 1 2 3");

                using (var waveOut = new WaveOut { Device = new WaveOutDevice(deviceId) })
                using (var waveSource = new MediaFoundationDecoder(stream))
                {
                    waveOut.Initialize(waveSource);
                    waveOut.Play();
                    waveOut.WaitForStopped();
                }
            }
        }
    }
}