2017-05-04 2 views
3

Microsoft 음성 합성을 사용 중이며 선택한 출력 오디오 장치로 출력을 리디렉션하고 싶습니다.Speech.Synthesizer를 특정 장치에 보냅니다.

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

현재 내가 사용하고 있습니다 :

지금까지 나는 다음과 같은 코드가

speechSynthesizer.SetOutputToDefaultAudioDevice(); 

을하지만 사실은 내 선택의 장치로 보내려고합니다. 내가 선택한 출력 장치를 지시하는 방법에 대한 cscore 예제를 찾고있다. 나는 내가 사용할 수있는 것을 본다 :

이것은 "Stream"을 취하지 만 나는 그것을 먹이는 법을 모른다.

감사합니다.

+0

그래서 'Stream'에 데이터가 있습니까? 데이터의 형식은 무엇입니까? –

답변

2

MemoryStream을 만들고 CSCore의 WaveOut 클래스에 연결할 수 있습니다. WaveOut에는 IWaveSource 인수가 필요하므로 CSCore의 MediaFoundationDecoder을 사용하여 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(); 
       } 
      } 
     } 
    } 
} 
+1

감사합니다. 매우 간결하고 도움이되는 답변 - 많이 감사드립니다! –

관련 문제