我有一个播放声音的按钮。当我多次按下按钮时,它会多次播放声音。没关系,我想要这个。但是当我单击停止按钮时,它必须停止当前播放的所有声音。我用了:
while (mediaPlayer.isPlaying()){mediaPlayer.stop();}
但它不起作用,声音继续播放。有人能帮我吗?这是我的完整代码:
public class HelloSoundboard extends Activity {
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button item1 = (Button)findViewById(R.id.item1);
item1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.atyourservice);
mediaPlayer.start();
}
});
Button stop = (Button)findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
while (mediaPlayer.isPlaying()){mediaPlayer.stop();}
// mediaPlayer.stop();
}
});
}
}