import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
/*
* 사운드 매니저를 이용한 사운드 관리
*/
public class SoundManager {
//필요한 필드 정의 하기
private static SoundManager sInstance;
private SoundPool soundPool;
private HashMap map;
private Context context;
//싱글톤 페턴 적용
private SoundManager(){} //객체를 생성할수 없도록 한다.
public static SoundManager getInstance(){
if(sInstance==null){
sInstance=new SoundManager();
}
return sInstance;
}
//초기화 하기
public void init(Context context){
this.context=context;
soundPool=new SoundPool(5,AudioManager.STREAM_MUSIC,0);
map=new HashMap();
}
//음원을 추가하는 메소드
public void addSound(int index,int resId){
int id=soundPool.load(context, resId,1);
//해당 아이디를 Map 에 저장한다
map.put(index, id);
}
//음원을 재생하는 메소드
public void play(int index){
soundPool.play(map.get(index), 1, 1, 1, 0, 1);
}
//재생을 중지하는 메소드
public void stopSound(int index){
soundPool.stop(map.get(index));
}
}
RECENT COMMENT