简体   繁体   中英

C# Speech Voice

I have created program in C# that converts text to speech but whatever gender I select it speaks same voice!!!Where gender is always same?I went to speech properties and it says I only have Microsoft Anna voice.

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

From the MSDN page for the SpeechSynthesizer.SelectVoiceByHints method:

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 if you select Female, then it will select Microsoft Anne. If you select Male, it will look for a male voice, but since you don't have any installed, then likely it is defaulting to the first voice of any gender... which would be Microsoft Anne, since that's the only one you have installed.

The process for installing voices isn't relevant for SO, as it has nothing to do with programming. Here's a solution on SuperUser that covers it, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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