在我的应用程序中,当用户点击一个拼写按钮时,它会播放音频以读出一个单词的字母并显示每个字母。它成功实施!
我用滑动手势来改变这个词。如果用户滑动屏幕,则应显示另一个单词。它也成功实施!!
问题:
如果用户滑动音频正在播放,那么音频应该停止并显示下一个单词。(我想正在运行的线程应该停止)。
我该怎么做?任何解决方案?
代码大纲:
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
}
} }
答案 0 :(得分:1)
我猜你不必破坏自己的线索。停止音乐mPlayVoice.stop();因为变量是在UIThread中声明的...它可以在UIThread中访问,你可以从那里停止你的播放器。很可能在处理程序中。
答案 1 :(得分:0)
mPlayVoice.stop()
http://developer.android.com/reference/android/media/MediaPlayer.html
/编辑
boolean shouldPlayVoice = true;
...
if(shouldPlayVoice){
mPlayVoice.start();
}
...
mPlayVoice.stop();
shouldPlayVoice = false;