我已經在C#中創建了將文本轉換爲語音的程序,但是我選擇它的性別會說同一個語音!!!性別總是相同的我去了語音屬性,它說我只有微軟安娜的聲音。C#語音語音
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);
}
如果您只安裝了一個語音播放語音,則該語音播放是唯一有意義的。 – Abion47
如何安裝其他聲音? –