1

私のプロジェクトはテキスト読み上げプロジェクトです。これが私のコードです。

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 を押すと、サウンドと u サウンドが再生されるだけです。背後にある理由は何ですか。助けてくれますか?

4

2 に答える 2

0

しかし、ブレークポイントを置いて F11 を押すと、サウンドと u サウンドが再生されるだけです。背後にある理由は何ですか。助けてくれますか?

問題は 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 に答える
0

の代わりにSoundPlayer.PlaySyncメソッドを使用しSoundPlayer.Playます。これにより、前のサウンドが終了した後に次のサウンドを開始できます。

于 2012-08-01T04:31:49.483 に答える