简体   繁体   English

如何在 ANDROID 中的以下条件下停止正在运行的线程

[英]How to stop a running thread under following condition in ANDROID

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 DrawLetter 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();停止音乐 mPlayVoice.stop(); since the variable is declared in UIThread... it can be accessed in UIThread and you can stop your player from there.因为该变量是在 UIThread 中声明的......它可以在 UIThread 中访问,您可以从那里停止播放器。 most probably in handler.最有可能在处理程序中。

 mPlayVoice.stop()

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

/edit /编辑

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM