using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;
namespace voicebutton
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SpeechSynthesizer speaker = new SpeechSynthesizer();
private void button1_Click(object sender, EventArgs e)
{
speaker.Speak("A as in apple");
speaker.Rate = -2;
speaker.SelectVoiceByHints(VoiceGender.Female);
}
}
}
我在做一个程序,当你点击一个按钮,它会播放声音。我的问题是,我将有26个按钮,我必须设置其率和性别一个接一个。有没有办法让我长话短说。我试着去上课,但行不通。有人可以帮助我或知道这个教程在网上。我找了一些,但找不到我需要的。
我也试图改变我的声音性别为女性(正如你可以看到上面的代码),但当我点击它在第一次它会播放一个男性的声音,但第二次点击后,它会播放女性。什么好主意吗?
提前感谢。
我不确定这是否是您想要的,但您可以将一个事件(如您的button1_Click
)设置为每个按钮,然后使用sender
属性来确定进一步的操作,如:
private static Dictionary<string, string> soundDictionary =
new Dictionary<string, string>();
private static void LoadDictionary()
{
soundDictionary.Add("a", "A as in apple.");
soundDictionary.Add("b", "B as in banana.");
}
private void PlaySound_Click(object sender, EventArgs e)
{
var button = (Button)sender;
var letter = button.Tag;
speaker.Rate = -2;
speaker.SelectVoiceByHints(VoiceGender.Female);
speaker.Speak(dictionary[letter]);
}
确保在PlaySound_Click
事件之前调用LoadDictionary()
方法。
对于说话者的性别,你可以尝试在Speak()
方法之前设置性别,如下所示:
speaker.Rate = -2;
speaker.SelectVoiceByHints(VoiceGender.Female);
speaker.Speak("A as in apple");
由于调用了speak方法,然后应用了女性性别配置,因此出现了性别语音问题。所以为了解决这个问题,你应该把配置然后调用speak方法,如下所示:
speaker.Rate = -2;
speaker.SelectVoiceByHints(VoiceGender.Female);
speaker.Speak("A as in apple");
为了使代码更短首先你应该创建一个方法,接受Text和Gender它会执行speak,就像
下面public void speak(String toSpeak, VoiceGender gender){
SpeechSynthesizer speaker = new SpeechSynthesizer();
speaker.Rate = -2;
speaker.SelectVoiceByHints(gender);
speaker.Speak(toSpeak );
}
您可以使用if语句来检查哪个按钮引发了maddin在他的回答中提到的事件,然后只需调用上面的函数
只需对每个按钮使用相同的函数,并创建一个
string buttonName = ((Button)sender).Name;
string speechText = string.Empty;
if (buttonName == "Button1"))
{
speechText = "A as in apple";
} else if
...
speaker.Speak(speechText );
speaker.Rate = -2;
speaker.SelectVoiceByHints(VoiceGender.Female);
如果你使用Dictionary<string,string>
甚至Dictionary<Control,string>
,你甚至可以改进它
:
//in Form.Designer.cs
public partial class Form1 {
private InitializeComponent() {
//Code of the Designer
this.button1.Text = "My Fancy Button";
this.button1.Click += this.button_click;
//Code of the Designer
this.button2.Text = "My Other Fancy Button";
this.button2.Click += this.button_click;
//Code of the Designer
}
}
//in Form.cs
public class Form1 : Form {
//Constructor
public Form1 () {
InitializeComponent();
this.SetupSpeechTexts();
}
private Dictonary<Control, string> speechTextDict = new Dictonary<Control, string>();
private void SetupSpeechTexts() {
this.speechTextDict.Add(this.button1, "First Text");
this.speechTextDict.Add(this.button2, "Second Text");
...
}
private void button_click(object sender, EventArgs e) {
Control senderControl = (Control)sender;
if(this.speechTextDict.ContainsKey(senderControl)) {
speaker.Speak(this.speechTextDict[senderControl]);
speaker.Rate = -2;
speaker.SelectVoiceByHints(VoiceGender.Female);
}
}
}