下面的條件下停止正在運行的線程,當用戶單擊某個拼寫按鈕時,它會播放音頻以顯示每個字母來讀出一個單詞的字母。它成功實施!如何在Android應用程序的ANDROID
而我用滑動手勢來改變這個詞。如果用戶滑動屏幕,則應該顯示另一個單詞。並且它也成功實施了!
問題:
而如果用戶刷那麼音頻應停止並應和顯示下一個單詞(我猜正在運行的線程應該停止)音頻播放。
我該怎麼做?任何方案?
代碼概述:
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()停止mediya的球員,但同時線程再次運行時它開始 – vnshetty
我不太明白你的代碼。爲什麼MediaPlayer會在你的線程的循環中開始?你在播放多個文件嗎?考慮更新你的問題,使其更清楚。 –
ok ....在UI線程內初始化你的mPlayVoice,而不是初始化成新線程的run方法。像mPlayVoice = MediaPlayer.create(BirdsActivity.this,mAlphabetsSound [letterPosition]);在UIThread中執行此操作。我的意思是在你的runOnUIThread Line之上的代碼中。 ..我想它現在應該可以解決你的問題 –