AsyncTask を使用して、バックグラウンドで 5 つの SoundPool を次々に再生しようとしています。しかし、サウンドの実行中に ImageView をクリックしたいのです。問題は、SoundPool が getBaseContext() を使用して、doInBackground の実行中に ImageView をブロックするときに始まります。何が起こっているか知っている人はいますか?どうも!これが私のコードです:
public class SensoriomotoraActivity extends Activity {
private boolean playing = false ;
private ImageView image ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sensoriomotora);
image = (ImageView) findViewById(R.id.img_sensoriomotora_pandereta);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (playing){
Log.v("onClick","clicked while playing");
}
else {
Log.v("onClick","clicked while !playing");
}
}
});
new Thread (
new Runnable(){
@Override
public void run(){
// Waiting 2.5 secs before playing the beeps
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
new PlaySoundBackground().execute();
}
});
}
}
).start();
}
private class PlaySoundBackground extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
playing = true ;
}
@Override
protected Void doInBackground(Void... params) {
SoundPool sound;
sound = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
sound.load(getBaseContext(), R.raw.beep, 1);
sound.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
for (int i=0 ; i<5; i++){
soundPool.play(sampleId, 1, 1, 1, 0, 1);
try {
Thread.sleep(2000); // Waits 2 secs before next beep
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
playing = false ;
}
}
}