我的项目是文本到语音项目,这是我的代码,
string amma = myacess.Text;
// string[] words = Regex.Split(amma,"*");
char[] delimeter = new char[] { '*' };
string[] words = amma.Split(delimeter, StringSplitOptions.RemoveEmptyEntries);
for( int i = 0; i < words.Length; i++ ) {
string audio = words[i];
if( audio == "a" ) {
SoundPlayer sndplayr = new SoundPlayer( WindowsFormsApplication1.Properties.Resources.aa );
sndplayr.Play();
}
if( audio == "u" ) {
SoundPlayer sndplayr = new SoundPlayer( WindowsFormsApplication1.Properties.Resources.i );
sndplayr.Play();
}
}
但输入文本"au",它将只播放"u"的声音。但我设置了断点并按下F11,然后只播放a音和u音。背后的原因是什么。你能帮我吗?
但我设置了断点并按下F11,然后只播放一个声音和u声音背后的原因是什么。你能帮我吗?
我认为问题是for循环太快了。
因此,您可以通过以下方式使用Timer
(System.Windows.Forms.Timer):
private Timer timer;
private int i;
string amma = myacess.Text;
// string[] words = Regex.Split(amma,"*");
char[] delimeter = new char[] { '*' };
string[] words = amma.Split(delimeter, StringSplitOptions.RemoveEmptyEntries);
//...
timer = new Timer();
timer.Interval = 100; //milliseconds
timer.Tick = += new EventHandler(timer_Tick);
timer.Enabled = true; //start the timer
i = 0;
void timer_Tick(object sender, EventArgs e){
if(i < word.Lenght){
string audio = words[i];
if( audio == "a" ) {
SoundPlayer sndplayr = new SoundPlayer( WindowsFormsApplication1.Properties.Resources.aa );
sndplayr.Play();
}
if( audio == "u" ) {
SoundPlayer sndplayr = new SoundPlayer( WindowsFormsApplication1.Properties.Resources.i );
sndplayr.Play();
}
i++; //increase i to change letter like in the loop
}
else{
timer.Enabled = false; //stop the timer
i = 0;
}
}
使用SoundPlayer.PlaySync
方法而不是SoundPlayer.Play
。通过此操作,您可以在上一个声音结束后开始下一个声音。