public class SoundExampleView extends View{
SoundPool m_SoundPool;
int m_Sound_id_1;
int m_Sound_id_2;
MediaPlayer m_Sound_Background;//배경음악
MediaPlayer m_sound_1;
MediaPlayer m_sound_2;
public SoundExampleView(Context context){
super(context);
m_SoundPool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
m_Sound_Background = MediaPlayer.create(context, R.raw.background2);
m_sound_1 = MediaPlayer.create(context, R.raw.effect1);
m_sound_2 = MediaPlayer.create(context, R.raw.effect2);
m_Sound_Background.start();
m_Sound_id_1 = m_SoundPool.load(context, R.raw.effect1,1);
m_Sound_id_2 = m_SoundPool.load(context, R.raw.effect2,2);
m_Sound_Background.start();
setFocusable(true);
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
//m_sound_1.start();
m_SoundPool.play(m_Sound_id_1, 1, 1, 0, 0, 1);
if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
//m_sound_2.start();
m_SoundPool.play(m_Sound_id_2, 1, 1, 0, 0, 1);
if(keyCode == KeyEvent.KEYCODE_SPACE){
if(m_Sound_Background.isPlaying())
m_Sound_Background.pause();
else
m_Sound_Background.start();
//화면을 갱신시켜준다.
invalidate();
}
return super.onKeyDown(keyCode, event);
}
protected void onDraw(Canvas canvas){
Paint p = new Paint();
p.setTextSize(20);
p.setColor(Color.WHITE);
String str;
//isPlaying 메서드를 통해 현재 상태를 얻어옵니다.
if(m_Sound_Background.isPlaying())
str="재생중";
else
str = "일시 정지줗ㅇ";
canvas.drawText("배경음악 : "+ str, 0, 20,p);
}
}
public class SoundExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SoundExampleView(this));
}
}