在我的应用程序中,当用户单击拼写按钮时,它会播放音频来读出单词的字母并显示每个字母。实现成功!
我使用滑动手势来更改单词。如果用户滑动屏幕,则应显示另一个单词。并且也成功实现了!!
问题:
在播放音频时,如果用户滑动,则音频应该停止并显示下一个单词。(我猜正在运行的线程应该停止)。
我该怎么做?有什么解决办法吗?
代码大纲:
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
}
} }
最佳答案
我想你不必自己销毁线程。停止音乐 mPlayVoice.stop();由于该变量是在 UIThread 中声明的...它可以在 UIThread 中访问,并且您可以从那里停止播放器。最有可能在处理程序中。
关于java - 如何在ANDROID中的以下条件下停止正在运行的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688395/