[英]Sound stops when button is pressed again
I m trying to make a android soundboard.When i touch any button it produces sound but when i touch it again, not only sound stops but none of the other button works.I want it play one sound at a time.Here is my main activity im calling the play functions on buttons.我正在尝试制作一个 android 音板。当我触摸任何按钮时,它会发出声音,但是当我再次触摸它时,不仅声音停止,而且其他按钮都不起作用。我希望它一次播放一个声音。这是我的主要内容活动即时调用按钮上的播放功能。
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();
}
}
This sample is taken from your last method:此示例取自您的最后一个方法:
public void playchup(View view) {
if (cry.isPlaying())
cry.stop();
if (whine.isPlaying())
whine.stop();
if (weed.isPlaying())
weed.stop();
chup.start();
}
If you look at the last three if-statements you can see that you stop the sound.如果您查看最后三个 if 语句,您可以看到您停止了声音。 If any sound is playing it is stopped when the button is pressed.
如果正在播放任何声音,则按下按钮时它会停止。
Let me further explain:让我进一步解释:
Possible issue可能的问题
In theory, when the MediaPLayer is played through, you have to recreate it.理论上,当 MediaPLayer 播放完毕时,您必须重新创建它。 See this question to see how you can loop the sounds and not have to recreate the mediaplayer every single time.
请参阅此问题以了解如何循环播放声音而不必每次都重新创建媒体播放器。
Meaning your code should look like this:这意味着您的代码应如下所示:
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);
And if you don't want to stop every other sound when one button is pressed you have to remove the if-statements that check if other sounds are playing and stopping them.如果您不想在按下一个按钮时停止所有其他声音,则必须删除检查其他声音是否正在播放并停止播放的 if 语句。
Alternative to MediaPlayer媒体播放器的替代品
If you are dealing with several sounds you can use a SoundPool as it is designed to handle several and overlapping sounds at once.如果您要处理多个声音,则可以使用SoundPool,因为它旨在同时处理多个重叠的声音。
Because You are creating MusicPlayer
objects but not releasing them因为您正在创建
MusicPlayer
对象但没有释放它们
Declare a Interface and override setOnCompletion
to call release()
method.声明一个接口并覆盖
setOnCompletion
以调用release()
方法。
Example例子
private class musicCompletionListener implements MediaPlayer.OnCompletionListener {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
}
and Then setOnCompletion listener for each mediaPlayer objects.然后为每个mediaPlayer对象 setOnCompletion 侦听器。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.