1

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

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

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 中读取文本。目前,合成器可以工作,但我想将此功能添加到我的文本到语音功能中。

谢谢。

4

1 回答 1

3

这段代码应该大致完成你所追求的(如果你仍然感兴趣的话:)。您需要将一个名为“comboBox1”的新组合框拖到表单上

    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");
    }
于 2013-09-20T11:56:37.460 回答