我的项目是文本到语音项目,这是我的代码,
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然后只播放声音和声音。背后的原因是什么?你能帮助我吗?
答案 0 :(得分:0)
但我放了断点并按F11然后它只播放声音而你 声音。背后的原因是什么?你能帮助我吗?
我认为问题在于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;
}
}
答案 1 :(得分:0)
使用SoundPlayer.PlaySync
Method代替SoundPlayer.Play
。通过这个,您可以在上一个声音结束后开始下一个声音。