我想通過onTouch連續播放槍聲。我正在這樣做自動槍聲。所以我有問題,有延遲的聲音循環。這並沒有給出真正的自動槍聲效果。我的主要觀點是,聲音一再播放時不應有任何延遲。 我的代碼是。連續播放聲音onTouch()
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mediaPlayer = MediaPlayer.create(RipleFire.this,
sounds[position]);
// if (mediaPlayer != null && mediaPlayer.isPlaying()) {
// mediaPlayer.stop();
// }
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imgflag.setImageResource(pistols[position]);
imgflag.setImageResource(pistolsAnimations[position]);
mediaPlayer.start();
mediaPlayer.setLooping(true);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
imgflag.setImageResource(pistols[position]);
}
return true;
}
嘗試將您的代碼放入runOnUiThread()檢查:http:// stackoverflow。com/questions/27039503/update-textview-volume-up-key-is-pressed/27039696#27039696 – 2014-11-22 05:53:25
HareshChhelana runOnUiThread()對我來說不起作用。 – Rayyan 2014-11-22 06:41:23