2017-01-03 208 views
0

我已經在C#中創建了將文本轉換爲語音的程序,但是我選擇它的性別會說同一個語音!!!性別總是相同的我去了語音屬性,它說我只有微軟安娜的聲音。C#語音語音

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

using System.Speech.Synthesis; 
using System.Speech.Recognition; 
using System.IO; 
using System.Diagnostics; 

namespace Speech_Recognition___Text_to_Speech 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
    SpeechSynthesizer voice = new SpeechSynthesizer(); 
      private void Form1_Load(object sender, EventArgs e) 
      { 
       comboBox1.Text = "NotSet"; 
       button2.Enabled = false; 
       button3.Enabled = false; 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       //Read (button_click) 
       voice.Rate = SpeedTrackBar.Value; //sets speed 
       voice.Volume = VolumeTrackBar.Value; //sets volume 
       try 
       { 
        switch (comboBox1.SelectedIndex) 
        { 
         case 0: 
          voice.SelectVoiceByHints(VoiceGender.NotSet); 
          break; 
         case 1: 
          voice.SelectVoiceByHints(VoiceGender.Male); 
          break; 
         case 2: 
          voice.SelectVoiceByHints(VoiceGender.Female); 
          break; 
         case 3: 
          voice.SelectVoiceByHints(VoiceGender.Neutral); 
          break; 
         default: 
          break; 
        } 
        voice.SpeakAsync(textBox1.Text); 
        button2.Enabled = true; 
        button3.Enabled = true; 
       } 
       catch(Exception ex) 
       { 
        MessageBox.Show(ex.Message, "Mevoiceage", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
      } 

      private void button2_Click(object sender, EventArgs e) 
      { 
       //Pause (button_click) 
       voice.Pause(); 
      } 

      private void button3_Click(object sender, EventArgs e) 
      { 
       //Continue (button_click) 
       voice.Resume(); 
      } 

      private void button4_Click(object sender, EventArgs e) 
      { 
       //Record (button_click) 
       //SpeechSynthesizer voice = new SpeechSynthesizer(); 
       voice.Rate = SpeedTrackBar.Value; 
       voice.Volume = VolumeTrackBar.Value; 
       SaveFileDialog sfd = new SaveFileDialog(); 
       sfd.Filter = "Wave Files| *.wav"; 
       sfd.ShowDialog(); 
       voice.SetOutputToWaveFile(sfd.FileName); 
       voice.Speak(textBox1.Text); 
       voice.SetOutputToDefaultAudioDevice(); 
       MessageBox.Show("Recording Completed..", "T2S"); 
      } 

      private void button6_Click(object sender, EventArgs e) 
      { 
       try 
       { 

        OpenFileDialog ofd = new OpenFileDialog(); 

        ofd.CheckFileExists = true; 
        ofd.CheckPathExists = true; 
        ofd.DefaultExt = "txt"; 
        ofd.DereferenceLinks = true; 
        ofd.Filter = "Text files (*.txt)|*.txt|" + "RTF files (*.rtf)|*.rtf|" + "Works 6 and 7 (*.wps)|*.wps|" + 
             "Windows Write (*.wri)|*.wri|" + "WordPerfect document (*.wpd)|*.wpd"; 
        ofd.Multiselect = false; 
        ofd.RestoreDirectory = true; 
        ofd.ShowHelp = true; 
        ofd.ShowReadOnly = false; 
        ofd.Title = "select a file"; 
        ofd.ValidateNames = true; 
        if (ofd.ShowDialog() == DialogResult.OK) 
        { 

         StreamReader sr = new StreamReader(ofd.OpenFile()); 
         textBox1.Text = sr.ReadToEnd(); 
        } 

       } 
       catch (Exception) 
       { 
        MessageBox.Show("can not open the file", "Text to Speak", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
      } 

      private void button7_Click(object sender, EventArgs e) 
      { 
       voice.SpeakAsyncCancelAll(); 
      } 

      private void button8_Click(object sender, EventArgs e) 
      { 
       textBox1.Text = ""; 
      } 

      private void button5_Click(object sender, EventArgs e) 
      { 
       Process.Start("https://www.google.com/#q=" + textBox1.Text); 
      } 
+0

如果您只安裝了一個語音播放語音,則該語音播放是唯一有意義的。 – Abion47

+0

如何安裝其他聲音? –

回答

0

從爲SpeechSynthesizer.SelectVoiceByHints方法MSDN頁:

Use the GetInstalledVoices method and VoiceInfo class to obtain the names of installed text-to-speech (TTS) voices that you can select. The SpeechSynthesizer object selects the first installed voice whose Gender property matches the gender parameter.

所以,如果你選擇女性,那麼它會選擇微軟安妮。如果您選擇男性,它會尋找男性的聲音,但由於您沒有安裝任何設備,因此很可能是第一個聲音任何性別...這將是微軟安妮,因爲這是唯一的你已經安裝了一個。

安裝語音的過程與SO無關,因爲它與編程無關。 Here's a solution on SuperUser that covers it, though.

+0

我已經按照你給我的網站的步驟。現在屬性可以提供兩個更多的聲音,但是當我想從微軟安娜轉換到安裝的聲音,它給了我錯誤:語音無法播放。請嘗試選擇另一個語音或選擇一個不同的音頻輸出設備。 –

+0

@Pavle_nis就像我說的,這是一個超級用戶問題。在那邊你會得到更好的運氣。 – Abion47