简体   繁体   English

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

[英]How to play all words using narrator?

my project is text to speech project, here is my code, 我的专案是文字转语音专案,这是我的程式码,

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();
    }
}

but enter text "au" it will play only "u" sound. 但是输入文本“ au”,它将仅播放“ u”声音。 but I put break point and press F11 then only it's play a sound and u sound. 但是我放了断点,然后按F11键,只有它在播放声音,然后才发出声音。 What is the reason behind. 背后是什么原因。 please can you help me? 请你能帮我吗?

but I put break point and press F11 then only it's play a sound and u sound. 但是我放了断点,然后按F11键,只有它在播放声音,然后才发出声音。 What is the reason behind. 背后是什么原因。 please can you help me? 请你能帮我吗?

I think that the problem is that the for-loop is too fast. 我认为问题在于for循环太快。

So you can use a Timer (System.Windows.Forms.Timer) in this way: 因此,您可以通过以下方式使用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;
    }
}

Use SoundPlayer.PlaySync Method instead of SoundPlayer.Play . 使用SoundPlayer.PlaySync方法代替SoundPlayer.Play by this you can start next sound after previous sound ended. 这样,您可以在上一个声音结束后开始下一个声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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