1

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. but I put break point and press F11 then only it's play a sound and u sound. What is the reason behind. please can you help me?

4

2 回答 2

0

使用SoundPlayer.PlaySync方法而不是SoundPlayer.Play. 这样,您可以在上一个声音结束后开始下一个声音。

于 2012-08-01T04:31:49.483 回答
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;
    }
}
于 2012-08-01T03:59:41.510 回答