我尝试使用以下代码。我的应用程序是儿童学习应用程序,在那里我设置了许多声音。单击一个按钮时,它正在播放,但同时,在第一次声音结束之前,另一个按钮的单击不起作用。我想释放第一个声音,同时另一个按钮将同时单击。
public void soreo (View v){
Button button = (Button)findViewById(R.id.Soreo);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.soreo);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}
public void sorea (View v){
Button button = (Button)findViewById(R.id.Sorea);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.sorea);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}
public void stopPlayer (){
if (player!=null){
player.stop();
player.reset();
player.release();
player=null;
}
}
@Override
protected void onStop (){
super.onStop();
stopPlayer();
}
您能否编辑并解释我将如何解决该问题?谢谢。
我建议每次单击按钮时停止播放器。您可以考虑以下实现。