1
我有一個應用程序替換圖像並單擊按鈕播放聲音,我希望做的是一旦聲音恢復爲原始圖像已經停止播放。聲音停止播放後,在android/Java中替換圖像
我的按鈕,點擊收聽:
public void onClick(View v) {
// Perform action on click
Context context = getApplicationContext();
CharSequence text = "Playing Theme";
int duration = Toast.LENGTH_SHORT;
//this is the replaced image while the sound is playing
imgSheep.setImageResource(R.drawable.replacedimage);
Toast.makeText(context, text, duration).show();
playSound(R.drawable.sound);
}
我的聲音播放功能:
//plays a sound file
private void playSound(int sFile) {
//set up MediaPlayer
final int medFile = sFile;
Thread thread = new Thread(new Runnable() {
public void run() {
playSound = MediaPlayer.create(getApplicationContext(), medFile);
playSound.start();
}
});
thread.start();
}
我知道我可以使用的方法,如:
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
performOnEnd();
}
});
所以我可以有它是這樣的:
playSound.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer playSound) {
imgSheep.setImageResource(R.drawable.originalimage);
}
});
它看起來像你一般知道如何做到這一點。那麼你有什麼問題? –
我在onCreate實例中添加並運行該應用程序給我一個FC錯誤... – Si8
從日誌和您的onCreate的代碼發佈堆棧跟蹤。 –