无法更改system.speech语音 [英] Can't change system.speech voice

查看:137
本文介绍了无法更改system.speech语音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个"语音机器人"。我想改变我正在使用的声音:

I am making a "voice bot" and i want to change the voice this is what I'm using:

s.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior);

它适用于互联网上的每个人,但它不适用于我,我没有收到任何错误。

It works for everyone on the internet but it won't work for me, I'm not getting any errors.

我在视觉工作室使用C# 2017年在Windows 7上。

I am using C# in visual studio 2017 on windows 7.

谢谢。

推荐答案

您好Mestroyer,

Hi Mestroyer,

感谢您发布此处。

如果您有任何问题,请尝试以下代码。我在Win7上测试。它适用于我。

For your question, please try the code below. I test on Win7. It works well for me.

using System.IO;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace System.Speech_voice
{
    public partial class Form1 : Form
    {
        private SpeechSynthesizer speech;
        /// <summary>
        /// volume
        /// </summary>
        private int value = 100;
        /// <summary>
        /// speed
        /// </summary>
        private int rate;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text;
            if (text.Trim().Length == 0)
            {
                MessageBox.Show("Can not read null content!", "Error");
                return;
            }
            if (button1.Text.Equals("Play voice"))
            {
                speech = new SpeechSynthesizer();
                new Thread(Speak).Start();
                button1.Text = "Stop hearing";
            }
            else if (button1.Text.Equals("Stop hearing"))
            {
                speech.SpeakAsyncCancelAll();//Stop Speaking
                button1.Text = "Play voice";
            }
        }
        private void Speak()
        {
            speech.Rate = rate;
            speech.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior);//Set up announcer (Chinese) 
            //speech.SelectVoice("Microsoft Anna"); //English
            speech.Volume = value;
            speech.SpeakAsync(textBox1.Text);//Speech reading method 
            speech.SpeakCompleted += speech_SpeakCompleted;
        }
        /// <summary>
        /// This event will be sent after the completion of the speech reading 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            button1.Text = "Play voice";
        }
        /// <summary>
        /// Drag progress bar event 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            value = trackBar1.Value * 10;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text;
            if (text.Trim().Length == 0)
            {
                MessageBox.Show("Empty content can not be generated!", "Error");
                return;
            }
            this.SaveFile(text);
        }
        /// <summary>
        /// A method of generating a voice file 
        /// </summary>
        /// <param name="text"></param>
        private void SaveFile(string text)
        {
            speech = new SpeechSynthesizer();
            var dialog = new SaveFileDialog();
            dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";
            dialog.ShowDialog();
            string path = dialog.FileName;
            if (path.Trim().Length == 0)
            {
                return;
            }
            speech.SetOutputToWaveFile(path);
            speech.Volume = value;
            speech.Rate = rate;
            speech.Speak(text);
            speech.SetOutputToNull();
            MessageBox.Show("Succed! file in"+path);
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            rate = Int32.Parse(comboBox1.Text);    
        }
        private void OpneLocalFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ReadlocalFile();
        }
        /// <summary>
        /// OpneLocalFile
        /// </summary>
        private void ReadlocalFile()
        {
            var open = new OpenFileDialog();
            open.ShowDialog();
            string path = open.FileName;
            if (path.Trim().Length == 0)
            {
                return;
            }
            var os = new StreamReader(path, Encoding.UTF8);
            string str = os.ReadToEnd();
            textBox1.Text = str;
        }
        private void ClearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}

最好的问候,

Wendy


这篇关于无法更改system.speech语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆