android 同时播放多个音频,在Android中同时播放多个声音

有以下课程使用声音池播放声音fx:

public class SoundManager {

public static int SOUNDPOOLSND_MENU_BTN = 0;

public static int SOUNDPOOLSND_WIN = 1;

public static int SOUNDPOOLSND_LOOSE = 2;

public static int SOUNDPOOLSND_DRAW = 3;

public static int SOUNDPOOLSND_TICK1 = 4;

public static int SOUNDPOOLSND_TICK2 = 5;

public static int SOUNDPOOLSND_OUT_OF_TIME = 6;

public static int SOUNDPOOLSND_HISCORE = 7;

public static int SOUNDPOOLSND_CORRECT_LETTER = 8;

public static boolean isSoundTurnedOff;

private static SoundManager mSoundManager;

private SoundPool mSoundPool;

private SparseArray mSoundPoolMap;

private AudioManager mAudioManager;

public static final int maxSounds = 4;

public static SoundManager getInstance(Context context)

{

if (mSoundManager == null){

mSoundManager = new SoundManager(context);

}

return mSoundManager;

}

public SoundManager(Context mContext)

{

mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);

mSoundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0);

// mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

// public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {

// loaded = true;

// }

// });

mSoundPoolMap = new SparseArray();

mSoundPoolMap.put(SOUNDPOOLSND_MENU_BTN, mSoundPool.load(mContext, R.raw.menubutton, 1));

mSoundPoolMap.put(SOUNDPOOLSND_WIN, mSoundPool.load(mContext, R.raw.win, 1));

mSoundPoolMap.put(SOUNDPOOLSND_LOOSE, mSoundPool.load(mContext, R.raw.lose, 1));

mSoundPoolMap.put(SOUNDPOOLSND_TICK1, mSoundPool.load(mContext, R.raw.tick_0, 1));

mSoundPoolMap.put(SOUNDPOOLSND_TICK2, mSoundPool.load(mContext, R.raw.tick_1, 1));

mSoundPoolMap.put(SOUNDPOOLSND_OUT_OF_TIME, mSoundPool.load(mContext, R.raw.out_of_time, 1));

mSoundPoolMap.put(SOUNDPOOLSND_HISCORE, mSoundPool.load(mContext, R.raw.personal_highscore, 1));

mSoundPoolMap.put(SOUNDPOOLSND_CORRECT_LETTER, mSoundPool.load(mContext, R.raw.correct_letter, 1));

// testing simultaneous playing

int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

mSoundPool.play(mSoundPoolMap.get(0), streamVolume, streamVolume, 1, 20, 1f);

mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 2, 1f);

mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f);

}

public void playSound(int index) {

if (isSoundTurnedOff)

return;

int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);

}

public static void clear()

{

if (mSoundManager != null){

mSoundManager.mSoundPool = null;

mSoundManager.mAudioManager = null;

mSoundManager.mSoundPoolMap = null;

}

mSoundManager = null;

}

}

它同时播放评论“测试同时播放”下面提到的3个声音.看来你需要在我的代码监听器(OnLoadCompleteListener)中实现注释.可能会发生系统只是在你开始播放时没有加载声音.如果您不想实现提到的侦听器,请尝试在播放声音活动开始后等待一段时间,并在一段时间后开始播放声音.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值