- 게임에서 다양한 효과음을 멀티체널로 호출할 수 있는 SoundManager 클래스입니다.
소스는 구글링중에 예제소스 수정하였습니다.
[SoundManager.java]
package kr.co.joynuri.animalorchestra;
import java.util.Collection;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
public class SoundManager {
private static final String LOG_TAG = SoundManager.class.getSimpleName();
private Context mContext;
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
public SoundManager(Context context) {
mContext = context;
}
public void create() {
mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void destroy() {
if (mSoundPoolMap != null) {
Collection<Integer> soundIds = mSoundPoolMap.values();
for (int soundId : soundIds) {
mSoundPool.unload(soundId);
Log.d(LOG_TAG, "destroy sound id " + soundId);
}
mSoundPoolMap = null;
}
}
public void load(int key, int resId) {
Log.d(LOG_TAG, "load...START");
mSoundPoolMap.put(key, mSoundPool.load(mContext, resId, 1));
Log.d(LOG_TAG, "load...END");
}
public void play(int key) {
Log.d(LOG_TAG, "play...START");
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(
mSoundPoolMap.get(key),
streamVolume, streamVolume,
1, 0, 1f);
Log.d(LOG_TAG, "play...END");
}
public void playLoop(int key) {
Log.d(LOG_TAG, "playLoop...START");
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(
mSoundPoolMap.get(key),
streamVolume, streamVolume,
1, -1, 1f);
Log.d(LOG_TAG, "playLoop...END");
}
public void stop(int key) {
mSoundPool.stop(mSoundPoolMap.get(key));
}
public void pause(int key) {
mSoundPool.pause(mSoundPoolMap.get(key));
}
public void resume(int key) {
mSoundPool.resume(mSoundPoolMap.get(key));
}
}
사용예) 사운드 등록
int coundIndex = 0;
mSoundManager = new SoundManager(mContext);
mSoundManager.create();
mSoundManager.load(coundIndex++, R.raw.snd_animal_0);
mSoundManager.load(coundIndex++, R.raw.snd_animal_1);
mSoundManager.load(coundIndex++, R.raw.snd_animal_2);
mSoundManager.load(coundIndex++, R.raw.snd_animal_3);
mSoundManager.load(coundIndex++, R.raw.snd_animal_4);
mSoundManager.load(coundIndex++, R.raw.snd_animal_5);
mSoundManager.load(coundIndex++, R.raw.snd_animal_6);
mSoundManager.load(coundIndex++, R.raw.snd_animal_7);
mSoundManager.load(coundIndex++, R.raw.snd_animal_8);
사용예) 사운드 해제
mSoundManager.destroy();
사용예) 사운드 플레이
mSoundManager.play(index);
'Android > Tip&Tech' 카테고리의 다른 글
intent 객체 직렬화 해서 보내기(Bundle이용 포함) (0) | 2010.11.30 |
---|---|
[안드로이드] Preference – UI 정보, 간단한 변수 저장하기 (0) | 2010.11.28 |
[펌]안드로이드 효과음 재생시 유용한 팁 (0) | 2010.11.26 |
안드로이드 백그라운드 설명서 pdf (0) | 2010.11.25 |
wifi를 이용한 심플싱크 (0) | 2010.11.25 |