mSoundManager.addSound(1,R.raw.dit);
mSoundManager.addSound(1,R.raw.dah);
당신은에 두 번째 줄을 변경해야합니다. SoundPool 선언에서 20 개의 스트림을 지정했습니다. 내 게임에는 많은 총과 나쁜 녀석이 소란을 피우며, 각각은 매우 짧은 사운드 루프 인 < 3000ms를 가지고 있습니다. 아래의 사운드를 추가 할 때 "mAvailibleSounds"라는 벡터의 지정된 인덱스를 추적합니다.이 방법을 사용하면 게임이 존재하지 않고 충돌없이 계속 실행되는 사운드를 재생할 수 있습니다. 이 경우 각 인덱스는 스프라이트 ID에 해당합니다. 특정 사운드를 특정 스프라이트에 매핑하는 방법을 이해하면됩니다.
다음으로 playSound()를 사용하여 소리를 대기열에 넣습니다. 이 때마다 soundId가 스택에 드롭되고 타임 아웃이 발생할 때마다 팝됩니다. 이렇게하면 재생 된 스트림을 죽이고 다시 사용할 수 있습니다. 내 게임이 매우 시끄 럽기 때문에 20 개의 스트림을 선택합니다. 그 후에 소리가 씻겨 나올 수 있으므로 각 응용 프로그램마다 마법 번호가 필요합니다.
이 소스는 here이며, 실행 큐 &을 직접 추가했습니다.
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);
}
}
크래시가 발생하는 예외는 무엇입니까? 스택 추적? –