我有以下代码:
if (someId.matches("A") || someId.matches("a")) {
tvLetCap.setText("A");
tvLetLow.setText("a");
ivLetterIcon.setImageResource(R.drawable.apple);
btnDisplayWord.setText("A is for APPLE");
mpSound = MediaPlayer.create(this, R.raw.sound);
mpSound.setLooping(false);
btnPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*try {
Uri uri = Uri.parse("android.resource://com.mypack.testing/" + R.raw.sound);
mpSound.setDataSource(getApplicationContext(),uri);
mpSound.prepare();
*/
mpSound.start();
btnPlay.setVisibility(View.GONE);
btnStop.setVisibility(View.VISIBLE);
btnStop.setOnClickListener(stopSound);
/*}
catch (IOException e){
Log.e("REPLAYING", "prepare() failed");
}*/
}
});
mpSound.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpSound.release();
btnPlay.setVisibility(View.VISIBLE);
btnStop.setVisibility(View.GONE);
}
});
}
它可以很好地处理注释掉的代码,但每当我尝试重播文件时,我的应用程序 FC.声音文件位于 res/raw/sound.mp3
如何修改代码,使其在按下 btnPlay
时播放多次。
最佳答案
您已经释放了媒体播放器。如果您想重复使用它,请不要这样做。
关于java - 单击按钮后重复播放声音多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18391192/