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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

2 Answers2

0

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?

I think that the problem is that the for-loop is too fast.

So you can use a Timer (System.Windows.Forms.Timer) in this way:

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;
    }
}
Omar
  • 16,329
  • 10
  • 48
  • 66
  • You're welcome. If it worked accept the answer, please. If you don't know how to do look here: [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) Thanks. – Omar Aug 01 '12 at 17:33
0

Use SoundPlayer.PlaySync Method instead of SoundPlayer.Play. by this you can start next sound after previous sound ended.

Ria
  • 10,237
  • 3
  • 33
  • 60