繁体   English   中英

如何使用旁白播放所有单词?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM