将 Speech.Synthesizer 发送到特定设备 [英] Send Speech.Synthesizer to a specific Device
问题描述
我正在使用 Microsoft Speech Synthesis 并希望将输出重定向到我选择的输出音频设备.
I'm using Microsoft Speech Synthesis and want to redirect the output to the output audio device of my choosing.
到目前为止,我有以下代码:
So far I have the following code:
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
speechSynthesizer.SpeakAsync("Yea it works!");
目前我正在使用:
speechSynthesizer.SetOutputToDefaultAudioDevice();
但我实际上想将它发送到我选择的设备.我正在寻找一个 cscore 示例,以了解如何指导我选择的输出设备.我看到我可以使用:
but I actually want to send it to the device of my choosing. I am looking for a cscore example for how to direct the output device of my choice. I see that I can use:
speechSynthesizer.SetOutputToWaveStream();
这需要一个流",但我不知道如何提供它.
This takes a "Stream", but I don't know how to feed it that.
谢谢.
推荐答案
您可以创建一个 MemoryStream 并将其附加到 CSCore 的 WaveOut 类.WaveOut 需要一个 IWaveSource 参数,因此您可以使用 CSCore 的 MediaFoundationDecoder 将波形流从SpeechSynthesizer.我做了一个小控制台应用程序来说明:
You can create a 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. I made a little console application to illustrate:
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();
}
}
}
}
}
这篇关于将 Speech.Synthesizer 发送到特定设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!