开发者

How to stop a running thread under following condition in ANDROID

开发者 https://www.devze.com 2023-03-20 00:17 出处:网络
in my app when user clicks on a spell button then it play audio to read outs letters of a word with displaying each letters. It is implemented successfully!.

in my app when user clicks on a spell button then it play audio to read outs letters of a word with displaying each letters. It is implemented successfully!.

And i used swipe gesture to change the word. if user swipes the screen then another word should be displayed. and It also implemented successfully!!

Problem:

While audio is playing if user swipes then the audio should stop and next word shold be displayed.(I guess the running thread should be stopped).

How can i do that? Any Solution?

Code Outlines:

public class BirdsActivity extends Activity implements OnClickListener{

//On createMethod*(){
.
.
  //on Spell button click run a thread to play audio and draw text
    new DrawLetter("AlphaKids").start();

// initiate Gesture detection

  }


class DrawLett开发者_StackOverflower extends Thread {
    String tempName=names[position];    
    String b="";
    int i=0;
    public DrawLetter(String str) {
    super(str);
    }
    public void run() {

        for (int i = 0; i < names[position].length(); i++) {
     runOnUiThread(new Runnable()
                {               
                     public void run() 
                  {     txtFrontName.setText(b); }

                });

            mPlayVoice = MediaPlayer.create(BirdsActivity.this, mAlphabetsSound[letterPosition]);
                mPlayVoice.start();
                try {
            sleep(500); 
            mPlayVoice.release();
            } catch (InterruptedException e) {}
     }}
.
.
.
  // Gesture detection class

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

      //if  valid swipe is performed
      {  
            //i want to stop(or Distroy) the above thread here

     }
} }


I guess you don't have to destroy the thread your self. Stop the music mPlayVoice.stop(); since the variable is declared in UIThread ... it can be accessed in UIThread and you can stop your player from there. most probably in handler.


 mPlayVoice.stop()

http://developer.android.com/reference/android/media/MediaPlayer.html

/edit

boolean shouldPlayVoice = true;
...
if(shouldPlayVoice){
mPlayVoice.start();
}
...
mPlayVoice.stop();
shouldPlayVoice = false;
0

精彩评论

暂无评论...
验证码 换一张
取 消