2014-01-27 31 views
2

所以我想改變System.Speech.Synthesis庫的C#語音。當我以控制檯模式嘗試代碼時,它將適用於我。但是,當我在Windows應用程序上工作時,它不會改變語音,同時不會發生錯誤。這是Windows應用程序的代碼,可以從語音變化中發揮作用。C#SelectVoice在Windows應用程序中沒有改變,但在控制檯中做

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Windows.Forms; 
using System.Speech.Synthesis; 
using System.Speech.Recognition; 

namespace JarvisRev1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      this.button1.Click += new EventHandler(button1_Click); 
      this.button2.Click += new EventHandler(button2_Click); 
      this.button3.Click += new EventHandler(button3_Click); 

      foreach (InstalledVoice voice in sSynth.GetInstalledVoices()) 
      { 
       cbVoice.Items.Add(voice.VoiceInfo.Name); 
      } 
     } 

     SpeechSynthesizer sSynth = new SpeechSynthesizer(); 
     PromptBuilder pBuilder = new PromptBuilder(); 
     SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine(); 


     private void button1_Click(object sender, EventArgs e) 
     { 
      pBuilder.ClearContent(); 
      pBuilder.AppendText(textBox1.Text); 
      sSynth.SelectVoice("IVONA 2 Brian"); 
      sSynth.SpeakAsync(pBuilder); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      button2.Enabled = false; 
      button3.Enabled = true; 
      Choices sList = new Choices(); 
      sList.Add(new string[] { "hello", "test", "it works", "how", "are", "you", "today", "i", "am", "fine", "exit", "close", "quit", "so", "hello how are you" }); 
      Grammar gr = new Grammar(new GrammarBuilder(sList)); 
      try 
      { 
       sRecognize.RequestRecognizerUpdate(); 
       sRecognize.LoadGrammar(gr); 
       sRecognize.SpeechRecognized += sRecognize_SpeechRecognized; 
       sRecognize.SetInputToDefaultAudioDevice(); 
       sRecognize.RecognizeAsync(RecognizeMode.Multiple); 
       sRecognize.Recognize(); 
      } 

      catch 
      { 
       return; 
      } 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      sRecognize.RecognizeAsyncStop(); 
      button2.Enabled = true; 
      button3.Enabled = false; 
     } 

     private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 


      if (e.Result.Text == "exit") 
      { 
       Application.Exit(); 
      } 
      else 
      { 
       textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString(); 
      } 

     } 

    } 
} 

這是在我工作的控制檯模式下的代碼。

using System; 
using System.Speech.Synthesis; // Add reference to System.Speech 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var synth = new SpeechSynthesizer(); 
     synth.SelectVoice("IVONA 2 Brian"); 
     synth.SpeakAsync("For you Sir, Always."); 
     foreach (var voice in synth.GetInstalledVoices()) 
     { 
      Console.WriteLine(voice.VoiceInfo.Name); 
     } 
     Console.ReadLine(); 
    } 
} 
+0

在此代碼cbVoice不工作 的foreach(InstalledVoice聲音在sSynth.GetInstalledVoices()){ ' cbVoice'.Items.Add(voice.VoiceInfo.Name); } –

回答

4

當微軟Irina Desktop語音在系統中可用時也有同樣的問題。解決方法在及時,e.g明確設置的聲音:

using System.Speech.Synthesis; 

var synth=new SpeechSynthesizer(); 
var builder=new PromptBuilder(); 
builder.StartVoice("Microsoft David Desktop"); 
builder.AppendText("Hello, World!"); 
builder.EndVoice(); 
synth.SpeakAsync(new Prompt(builder)); 

正如你已經利用PromptBuilder,嘗試添加StartVoiceEndVoice文本週圍通話。

+0

不知道問題在哪裏? – sarin

相關問題