How to play all words using narrator?

196 Views Asked by At

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?

2

There are 2 best solutions below

1
On

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;
    }
}
0
On

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