繁体   English   中英

C#语音语音

[英]C# Speech Voice

我已经在 C# 中创建了将文本转换为语音的程序,但无论我选择什么性别,它都会说相同的声音!性别总是相同的?我去了语音属性,它说我只有 Microsoft Anna 的声音。

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);
            }

SpeechSynthesizer.SelectVoiceByHints方法的 MSDN 页面:

使用 GetInstalledVoices 方法和 VoiceInfo 类获取您可以选择的已安装文本到语音 (TTS) 语音的名称。 SpeechSynthesizer 对象选择第一个安装的声音,其 Gender 属性与性别参数匹配。

因此,如果您选择女性,那么它将选择 Microsoft Anne。 如果您选择男性,它将寻找男性声音,但由于您没有安装任何声音,因此它可能默认为任何性别的第一个声音……这将是 Microsoft Anne,因为这是您唯一的声音已经安装。

安装声音的过程与 SO 无关,因为它与编程无关。 不过,这是 SuperUser 上的一个解决方案,涵盖了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM