如何停止活动中的所有声音? [英] How to stop all sounds in activity?

查看:48
本文介绍了如何停止活动中的所有声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序活动中有很多Mediaplayer声音,并且我有一个按钮来停止所有正在播放的声音,但是它占用了很多空间,我想知道代码如何同时停止所有mediplayer声音不喜欢这样:

I have a lot of Mediaplayer sounds in my application acitvity and i have button to stop all sounds that are playing but its taking a lot of space and i want to know the code how to stop all the mediplayer sounds at same time not like this:

    sadegfqc.pause();
    dsfsdf.pause();
    sadfsadfsad.pause();
    dsfg.pause();
    htzh.pause();
    nensmene.pause();
    fdshs.pause();
    gshtrhtr.pause();
    hfshztjr.pause();
    sgawg.pause();

然后我必须再次调用带有源的Mediaplayer的所有创建.像这样:

And then i have to call all the creates of Mediaplayer with a source again. like this:

dsfsadf= MediaPlayer.create(this, R.raw.dsfsadf);

推荐答案

SoundPool为此目的是一个更好的选择.我强烈警告不要实例化多个MediaPlayer实例,因为大多数系统没有资源来生成许多并行活动实例.您会发现在许多设备上按5次以上按钮都会导致基于内存的崩溃.

SoundPool is a much better alternative for this purpose. I would caution strongly against instantiating multiple MediaPlayer instances as most systems do not have the resources to generate many parallel active instances. You wil find on many device that hitting the button upwards of 5 times will cause a memory based crash.

就停止所有活动流而言,它没有内置功能,但是以类似于现有代码的方式很容易完成.附带说明一下,有一个autoPause()方法,该方法暂停所有流,但是并没有真正结束它们的播放(因为方法名称暗示).这是一个管理音频流的简单示例:

As far as stopping all active streams, there is not baked-in function for this, but it's easy to accomplish in a manner to similar to your existing code. As a side note, there is an autoPause() method, which halts all streams, but it doesn't truly end their playback (as the method name insinuates). Here is a simple example to manage your audio streams:

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

管理流ID值的列表比MediaPlayer实例的内存效率要高得多,您的用户将非常感谢您.另外,请注意,即使streamID不再有效,调用SoundPool.stop()也是安全的,因此您无需检查现有的播放.

It is much more memory efficient to manage a list of streamID values than MediaPlayer instances, and your users will thank you. Also, note that it is safe to call SoundPool.stop() even if the streamID is no longer valid, so you don't need to check for existing playback.

这篇关于如何停止活动中的所有声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆