MediaPlayer的如何停止播放多个声音 [英] MediaPlayer how to stop multiple sounds from playback

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

问题描述

我有一个按钮,播放声音。 当我按下按钮多次,它播放声音多次。 这是奥凯,我想这一点。 但是当我点击停止按钮,它必须停止所有声音当前播放。 我使用的:

 而(mediaPlayer.isPlaying()){mediaPlayer.stop();}
 

,但它不工作,声音再玩。有人可以帮我吗? 这里是我的全code:

 公共类HelloSoundboard延伸活动{
MediaPlayer的媒体播放器;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    按钮ITEM1 =(按钮)findViewById(R.id.item1);
    item1.setOnClickListener(新View.OnClickListener(){
       公共无效的onClick(视图查看){
           媒体播放器= MediaPlayer.create(getBaseContext(),R.raw.atyourservice);
           mediaPlayer.start();
       }
    });

    按钮停止=(按钮)findViewById(R.id.stop);
    stop.setOnClickListener(新View.OnClickListener(){
       公共无效的onClick(视图查看){
          而(mediaPlayer.isPlaying()){mediaPlayer.stop();}
        // mediaPlayer.stop();
       }
     });
 }
}
 

解决方案

的Soundpool 就是用于此目的的更好的选择。我要提醒强烈反对实例多个的MediaPlayer 实例作为大多数系统没有足够的资源来产生许多平行的活动实例。你港岛线找到许多装置击中的5倍的按钮向上将导致一个基于存储器崩溃。

至于停止所有活动流,没有烤函数对于这一点,但它很容易实现的方式类似于现有的code。作为一个方面说明,有一个 autoPause()的方法,它停止了所有流,但它并没有真正结束自己的播放(作为方法名影射)。下面是一个简单的例子来管理您的音频流:

  //初始化的Soundpool某处
的Soundpool池=新的Soundpool(10,AudioManager.STREAM_MUSIC,0);
//将您的声音效果入池
INT soundId = pool.load(...); //有好几个版本了这一点,选择哪些适合您的声音

名单<整数GT;流=新的ArrayList<整数GT;();
按钮ITEM1 =(按钮)findViewById(R.id.item1);
item1.setOnClickListener(新View.OnClickListener(){
   公共无效的onClick(视图查看){
       INT流ID = pool.play(soundId,1.0F,1.0F,1,0,1.0F);
       streams.add(流ID);
   }
});

按钮停止=(按钮)findViewById(R.id.stop);
stop.setOnClickListener(新View.OnClickListener(){
   公共无效的onClick(视图查看){
      对于(整数流:流){
          pool.stop(流);
      }
      streams.clear();
   }
});
 

这是更高效的内存管理流ID值的列表比的MediaPlayer 情况下,你的用户会感谢你。另外请注意,它是安全的,叫 SoundPool.stop()即使流ID不再有效,因此你不需要检查现有的播放。

心连心

i have a button that plays a sound. When i push the button multiple times, it plays the sound multiple times. This is oke, i want this. But when i click the stop button it must stop all sounds currently playing. I used:

   while (mediaPlayer.isPlaying()){mediaPlayer.stop();}

but it is not working, the sounds keep on playing. Can someone help me? Here is my full code:

public class HelloSoundboard extends Activity {
MediaPlayer mediaPlayer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button item1 = (Button)findViewById(R.id.item1);
    item1.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.atyourservice);
           mediaPlayer.start();
       }
    });

    Button stop = (Button)findViewById(R.id.stop);
    stop.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
          while (mediaPlayer.isPlaying()){mediaPlayer.stop();}
        //  mediaPlayer.stop();
       }
     });
 }
}

解决方案

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.

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

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.

HTH

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

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