使用 SoundManager 播放多种声音

发布于 2024-09-05 19:04:20 字数 755 浏览 8 评论 0原文

如果我播放单个声音,它运行得很好。

添加第二个声音会导致它崩溃。

任何人都知道是什么导致了这个问题?

private SoundManager mSoundManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sos);

    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());

    mSoundManager.addSound(1,R.raw.dit);
    mSoundManager.addSound(1,R.raw.dah);

    Button SoundButton = (Button)findViewById(R.id.SoundButton);
    SoundButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mSoundManager.playSound(1);
            mSoundManager.playSound(2);
        }
    });
}

If I play a single sound, it runs fine.

Adding a second sound causes it to crash.

Anyone know what is causing the problem?

private SoundManager mSoundManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sos);

    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());

    mSoundManager.addSound(1,R.raw.dit);
    mSoundManager.addSound(1,R.raw.dah);

    Button SoundButton = (Button)findViewById(R.id.SoundButton);
    SoundButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mSoundManager.playSound(1);
            mSoundManager.playSound(2);
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-09-12 19:04:20
 mSoundManager.addSound(1,R.raw.dit);
 mSoundManager.addSound(1,R.raw.dah);

您需要将第二行更改为:

mSoundManager.addSound(2,R.raw.dah);

为了一次播放多个声音,首先您需要让 SoundPool 知道这一点。请注意,在 SoundPool 的声明中,我指定了 20 个流。我的游戏中有很多枪和坏人发出噪音,并且每个都有一个非常短的声音循环,< 3000毫秒。请注意,当我在下面添加声音时,我会跟踪名为“mAvailibleSounds”的向量中的指定索引,这样我的游戏可以尝试为不存在的项目播放声音并继续运行而不会崩溃。在这种情况下,每个索引对应一个精灵 ID。只是为了让您了解我如何将特定声音映射到特定精灵。

接下来我们使用 playSound() 对声音进行排队。每次发生这种情况时,都会将 soundId 放入堆栈中,然后每当发生超时时我都会弹出该堆栈。这允许我在播放后终止流,然后再次重用它。我选择 20 个流,因为我的游戏非常嘈杂。之后声音就会消失,因此每个应用程序都需要一个幻数。

我在此处找到了这个源,并添加了可运行&我自己杀队列。

private  SoundPool mSoundPool; 
 private  HashMap<Integer, Integer> mSoundPoolMap; 
 private  AudioManager  mAudioManager;
 private  Context mContext;
 private  Vector<Integer> mAvailibleSounds = new Vector<Integer>();
 private  Vector<Integer> mKillSoundQueue = new Vector<Integer>();
 private  Handler mHandler = new Handler();

 public SoundManager(){}

 public void initSounds(Context theContext) { 
   mContext = theContext;
      mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0); 
      mSoundPoolMap = new HashMap<Integer, Integer>(); 
      mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);       
 } 

 public void addSound(int Index, int SoundID)
 {
  mAvailibleSounds.add(Index);
  mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

 }

 public void playSound(int index) { 
  // dont have a sound for this obj, return.
  if(mAvailibleSounds.contains(index)){

      int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
      int soundId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);

      mKillSoundQueue.add(soundId);

      // schedule the current sound to stop after set milliseconds
      mHandler.postDelayed(new Runnable() {
       public void run() {
        if(!mKillSoundQueue.isEmpty()){
         mSoundPool.stop(mKillSoundQueue.firstElement());
        }
          }
      }, 3000);
  }
 }
 mSoundManager.addSound(1,R.raw.dit);
 mSoundManager.addSound(1,R.raw.dah);

You need to change the second line to:

mSoundManager.addSound(2,R.raw.dah);

In order to play multiple sounds at once, first you need to let the SoundPool know that. In the declaration of SoundPool notice that I specified 20 streams. I have many guns and bad guys making noise in my game, and each has a very short sound loop, < 3000ms. Notice when I add a sound below I keep track of the specified index in a vector called, "mAvailibleSounds", this way my game can try and play sounds for items that dont exist and carry on without crashing. Each Index in this case corresponds to a sprite id. Just so you understand how I map particular sounds to specific sprites.

Next we queue up sounds, with playSound(). Each time this happens a soundId is dropped into a stack, which I then pop whenever my timeout occurs. This allows me to kill a stream after it has played, and reuse it again. I choose 20 streams because my game is very noisy. After that the sounds get washed out, so each application will require a magic number.

I found this source here, and added the runnable & kill queue myself.

private  SoundPool mSoundPool; 
 private  HashMap<Integer, Integer> mSoundPoolMap; 
 private  AudioManager  mAudioManager;
 private  Context mContext;
 private  Vector<Integer> mAvailibleSounds = new Vector<Integer>();
 private  Vector<Integer> mKillSoundQueue = new Vector<Integer>();
 private  Handler mHandler = new Handler();

 public SoundManager(){}

 public void initSounds(Context theContext) { 
   mContext = theContext;
      mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0); 
      mSoundPoolMap = new HashMap<Integer, Integer>(); 
      mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);       
 } 

 public void addSound(int Index, int SoundID)
 {
  mAvailibleSounds.add(Index);
  mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

 }

 public void playSound(int index) { 
  // dont have a sound for this obj, return.
  if(mAvailibleSounds.contains(index)){

      int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
      int soundId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);

      mKillSoundQueue.add(soundId);

      // schedule the current sound to stop after set milliseconds
      mHandler.postDelayed(new Runnable() {
       public void run() {
        if(!mKillSoundQueue.isEmpty()){
         mSoundPool.stop(mKillSoundQueue.firstElement());
        }
          }
      }, 3000);
  }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文