我可以使用组合框将我的语音合成器的声音更改为系统TTS语音之一吗?

时间:2013-04-29 19:56:14

标签: c# .net-4.0 text-to-speech speech-synthesis

我想在我的表单上有一个组合框,允许用户选择他们想要使用的语音。我该如何实现这样的功能?

目前,我的表单由四个按钮和一个组合框组成。按钮和合成器背后的代码如下:

private void button1_Click(object sender, EventArgs e)
        {
            reader.Dispose();
            if (Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text != "")
            {
                reader = new SpeechSynthesizer();
                reader.SpeakAsync(Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text)
                button2.Enabled = true;
                button4.Enabled = true;
                reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted);
            }
            else
            {
                MessageBox.Show("Please insert text before launching Text to Speech.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
        }
 private void button2_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Speaking)
                {
                    reader.Pause();
                    button3.Enabled = true;
                }
private void button3_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Paused)
                {
                    reader.Resume();
                }
                button3.Enabled = false;
            }
            }


        private void button4_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.Dispose();
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
            }

我想用一个当前安装的语音列表填充一个组合框,当用户点击一个时,它会从所选语音中的richTextBoxPrintCtrl1中读取文本。目前,合成器有效,但我想将此功能添加到我的文本到语音功能中。

感谢。

1 个答案:

答案 0 :(得分:2)

此代码应该大致完成您所追求的(如果您仍然感兴趣:)。您需要将名为“comboBox1”的新comboxbox拖到表单

    private SpeechSynthesizer reader = new SpeechSynthesizer();

    private void PopulateInstalledVoices()
    {
        foreach (InstalledVoice voice in
          reader.GetInstalledVoices(new CultureInfo("en-US")))
        {
            VoiceInfo info = voice.VoiceInfo;
            comboBox1.Items.Add(info.Name);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateInstalledVoices();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       var voice = comboBox1.SelectedItem as String;
       if (voice != null)
       {
           reader.SelectVoice(voice);
       }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        reader.SpeakAsync("this is a test message");
    }