안드로이드 개발 질문/답변
(글 수 45,052)
현재 원하는 기능 때문에 간단한 샘플을 만들었습니다.
하지만 생각대로 pause 가 이루어 지지 않습니다.
터치 이벤트( 다운 ) 의 경우 루프되는 사운드를 출력하고
터치 이벤트 ( 업 ) 의 경우 사운드를 멈추게 하려는 간단한것인데 생각되로 되지 않고
다시 다운 이벤트가 이루어 지면 그때 pause 가 이루어 집니다.
어느 부분이 잘 못 된 것일까요??;;;
import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView;
public class SoundPoolTest extends Activity {
public ImageView img1;
public ImageView img2;
public ImageView img3;
public SoundPool sound;
public int sound1;
public int sound2;
public int sound3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
public void init() {
sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0 );
sound1 = sound.load( getApplicationContext(), R.raw.sound1, 0);
sound2 = sound.load( getApplicationContext(), R.raw.sound2, 0);
sound3 = sound.load( getApplicationContext(), R.raw.sound3, 0);
img1 = (ImageView) findViewById( R.id.image1 );
img1.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
sound.play( sound1, 1, 1, 0, -1, 1);
break;
case MotionEvent.ACTION_UP:
sound.pause( sound1 );
break;
}
return true;
}
});
img2 = (ImageView) findViewById( R.id.image2 );
img2.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
sound.play( sound2, 1, 1, 0, -1, 1);
break;
case MotionEvent.ACTION_UP:
sound.pause( sound2 );
break;
}
return true;
}
});
img3 = (ImageView) findViewById( R.id.image3 );
img3.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
sound.play( sound3, 1, 1, 0, -1, 1);
break;
case MotionEvent.ACTION_UP:
sound.pause( sound3 );
break;
}
return true;
}
});
}
}




public class SoundPoolTest extends Activity { public SoundPool sound; public int s1; public Button button; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } public void init() { button = (Button)findViewById(R.id.button1); sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); s1 = sound.load( getApplicationContext(), R.raw.yuki3318, 0); button.setOnTouchListener(new OnTouchListener(){@Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch ( event.getAction() ) { case MotionEvent.ACTION_DOWN: h.sendEmptyMessage( PLAY ); break; case MotionEvent.ACTION_UP: h.sendEmptyMessage( STOP ); break; } return true; } }); } public Handler h = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch ( msg.what ) { case PLAY: Log.d("SoundPool", "sound PLAY"); int i = sound.play( s1, 1, 1, 0, -1, 1); if ( i == 0 ) Log.d("SoundPool", "failed..."); break; case STOP: Log.d("SoundPool", "sound Pause, Stop"); sound.stop( s1 ); s1 = sound.load( getApplicationContext(), R.raw.yuki3318, 0); break; } } }; public final static int PLAY = 1; public final static int STOP = 2; }결국 MotionEvent.ACTION_UP 에서 다시 로드를 해줬더니 원하는데로 진행이 됩니다.
원래 이렇게 되야 하는것인지......