android 播放声音 停止其他,android-MediaPlayer如何停止播放多个声音

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 streams = new ArrayList();

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();

}

});

与MediaPlayer实例相比,管理streamID值列表要节省内存,您的用户将非常感谢您.另外,请注意,即使streamID不再有效,也可以调用SoundPool.stop(),因此无需检查现有的播放.

HTH

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值