[英]How to play all words using narrator?
我的專案是文字轉語音專案,這是我的程式碼,
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鍵,只有它在播放聲音,然后才發出聲音。 背后是什么原因。 請你能幫我嗎?
但是我放了斷點,然后按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;
}
}
使用SoundPlayer.PlaySync
方法代替SoundPlayer.Play
。 這樣,您可以在上一個聲音結束后開始下一個聲音。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.