Java 再次按下按钮时,声音停止
我正在尝试制作一个android音板。当我触摸任何按钮时,它都会发出声音,但当我再次触摸它时,不仅声音停止,而且其他按钮都不起作用。我希望它一次播放一个声音。这是我的主要活动,我正在调用按钮上的播放功能Java 再次按下按钮时,声音停止,java,android,audio,Java,Android,Audio,我正在尝试制作一个android音板。当我触摸任何按钮时,它都会发出声音,但当我再次触摸它时,不仅声音停止,而且其他按钮都不起作用。我希望它一次播放一个声音。这是我的主要活动,我正在调用按钮上的播放功能 public class MainActivity extends AppCompatActivity { MediaPlayer whine, cry, weed, chup; @Override protected void onCreate(Bundle sav
public class MainActivity extends AppCompatActivity {
MediaPlayer whine, cry, weed, chup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
whine = MediaPlayer.create(this, R.raw.gone);
cry = MediaPlayer.create(this, R.raw.mock);
weed = MediaPlayer.create(this, R.raw.phen);
chup = MediaPlayer.create(this, R.raw.rg);
}
public void playwhine(View view) {
if (cry.isPlaying())
cry.stop();
if (weed.isPlaying())
weed.stop();
if (chup.isPlaying())
chup.stop();
whine.start();
}
public void playcry(View view) {
if (whine.isPlaying())
whine.stop();
if (weed.isPlaying())
weed.stop();
if (chup.isPlaying())
chup.stop();
cry.start();
}
public void playweed(View view) {
if (cry.isPlaying())
cry.stop();
if (whine.isPlaying())
whine.stop();
if (chup.isPlaying())
chup.stop();
weed.start();
}
public void playchup(View view) {
if (cry.isPlaying())
cry.stop();
if (whine.isPlaying())
whine.stop();
if (weed.isPlaying())
weed.stop();
chup.start();
}
}
此示例取自上一种方法:
public void playchup(View view) {
if (cry.isPlaying())
cry.stop();
if (whine.isPlaying())
whine.stop();
if (weed.isPlaying())
weed.stop();
chup.start();
}
如果你看最后三个If语句,你可以看到你停止了声音。如果正在播放任何声音,则按下按钮时停止播放
让我进一步解释:
private class musicCompletionListener implements MediaPlayer.OnCompletionListener {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
}
理论上,当MediaPLayer播放完毕时,您必须重新创建它。查看如何循环声音,而不必每次都重新创建mediaplayer
这意味着您的代码应该如下所示:
whine = MediaPlayer.create(this, R.raw.gone);
cry = MediaPlayer.create(this, R.raw.mock);
weed = MediaPlayer.create(this, R.raw.phen);
chup = MediaPlayer.create(this, R.raw.rg);
whine.setLooping(true);
cry.setLooping(true);
weed.setLooping(true);
chup.setLooping(true);
如果你不想在按下一个按钮时停止其他声音,你必须删除if语句,检查是否有其他声音在播放并停止它们
MediaPlayer的替代方案
如果您要处理多个声音,您可以使用a,因为它设计用于同时处理多个和重叠的声音 因为您正在创建
MusicPlayer
对象,而不是释放它们
声明一个接口并重写setOnCompletion
以调用release()
方法
示例
private class musicCompletionListener implements MediaPlayer.OnCompletionListener {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
}
然后为每个mediaPlayer对象设置完成侦听器
public void playwhine(View view) {
if (cry.isPlaying())
cry.stop();
if (weed.isPlaying())
weed.stop();
if (chup.isPlaying())
chup.stop();
whine.setOnCompletionListener(new musicCompletionListener());
whine.start();
}
public void playcry(View view) {
if (whine.isPlaying())
whine.stop();
if (weed.isPlaying())
weed.stop();
if (chup.isPlaying())
chup.stop();
cry.setOnCompletionListener(new musicCompletionListener());
cry.start();
}
public void playweed(View view) {
if (cry.isPlaying())
cry.stop();
if (whine.isPlaying())
whine.stop();
if (chup.isPlaying())
chup.stop();
weed.setOnCompletionListener(new musicCompletionListener());
weed.start();
}
public void playchup(View view) {
if (cry.isPlaying())
cry.stop();
if (whine.isPlaying())
whine.stop();
if (weed.isPlaying())
weed.stop();
chup.setOnCompletionListener(new musicCompletionListener());
chup.start();
}