当我按下我创建的操作栏上的静音按钮时,它仅停止播放的最后一个声音而不是所有声音。
此外,如果我按下按钮(5-6次)播放声音并按下同一活动上的静音,声音不会停止,然后返回并选择其他活动并按下应用程序崩溃的静音按钮。有什么想法吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
public void pb1(View view) {
mp = MediaPlayer.create(this, R.raw.sound1);
mp.start();
}
//inflates the menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menunot, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sound:
mp.stop();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.stop();
mp.reset();
mp.release();
mp = null;
}
答案 0 :(得分:0)
嘿,请尝试以下解决方案,
SoundPool是一个更好的替代方案。我强烈反对实例化多个MediaPlayer实例,因为大多数系统没有资源来生成许多并行活动实例。你会发现在许多设备上按下按钮超过5次会导致基于内存的崩溃。
就停止所有活动流而言,没有用于此功能的烘焙功能,但它很容易以类似于现有代码的方式完成。作为旁注,有一个autoPause()方法,它会暂停所有流,但它并没有真正结束它们的播放(方法名称暗示)。以下是管理音频流的简单示例:
//SoundPool initialization somewhere
SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
//Load your sound effect into the pool
int soundId = pool.load(...); //There are several versions of this, pick which fits your sound
List<Integer> streams = new ArrayList<Integer>();
Button item1 = (Button)findViewById(R.id.item1);
item1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int streamId = pool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamId);
}
});
Button stop = (Button)findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (Integer stream : streams) {
pool.stop(stream);
}
streams.clear();
}
});
管理streamID值列表的内存效率比MediaPlayer实例高得多,您的用户会感谢您。另请注意,即使streamID不再有效,也可以安全地呼叫SoundPool.stop(),因此您无需检查现有播放。
快乐编码:)
答案 1 :(得分:0)
只需制作一个数组列表然后停止所有, 看一下下面的例子:
private static final int steps[] =
{R.raw.step_1, R.raw.step_2, R.raw.step_3, R.raw.step_4, R.raw.step_5, R.raw.step_6, R.raw.step_7, R.raw.step_8, R.raw.step_9, R.raw.step_10
};
private int i = 0;
private ArrayList<MediaPlayer> voices = new ArrayList<>(10);
private Button btnPlay;
private Button btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(i==10)i=0;
final MediaPlayer voice = MediaPlayer.create(MainActivity.this, steps[i++]);
voice.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (voice != null) {
voices.remove(voice);
voice.release();
}
}
});
voice.start();
voices.add(voice);
}
});
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int j = voices.size() - 1; j >= 0; j--) {
if (voices.get(j) != null) {
if (voices.get(j).isPlaying())
voices.get(j).stop();
voices.get(j).release();
voices.remove(j);
}
}
}
});
}