적어도 Android 2.1에서 작동하는 호환 가능한 종류의 OnLoadCompleteListener
클래스를 구현했습니다.
생성자는 SoundPool
개체를 사용하며 SoundPool.load(..)
이 호출 된 사운드는 OnLoadCompleteListener.addSound(soundId)
으로 등록되어야합니다. 이 후 청취자는 주기적으로 요청 된 사운드를 재생하려고 시도합니다 (0 볼륨에서). 성공적으로 실행되면 Android 2.2 이상 버전과 마찬가지로 onLoadComplete
구현을 호출합니다. 여기
이 사용 예제 :
SoundPool mySoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
OnLoadCompleteListener completionListener = new OnLoadCompleteListener(mySoundPool) {
@Override
public void onLoadComplete(SoundPool soundPool, int soundId, int status) {
Log.i("OnLoadCompleteListener","Sound "+soundId+" loaded.");
}
}
int soundId=mySoundPool.load(this, R.raw.funnyvoice,1);
completionListener.addSound(soundId); // tell the listener to test for this sound.
그리고 여기 소스입니다 :
abstract class OnLoadCompleteListener {
final int testPeriodMs = 100; // period between tests in ms
/**
* OnLoadCompleteListener fallback implementation for Android versions before 2.2.
* After using: int soundId=SoundPool.load(..), call OnLoadCompleteListener.listenFor(soundId)
* to periodically test sound load completion. If a sound is playable, onLoadComplete is called.
*
* @param soundPool The SoundPool in which you loaded the sounds.
*/
public OnLoadCompleteListener(SoundPool soundPool) {
testSoundPool = soundPool;
}
/**
* Method called when determined that a soundpool sound has been loaded.
*
* @param soundPool The soundpool that was given to the constructor of this OnLoadCompleteListener
* @param soundId The soundId of the sound that loaded
* @param status Status value for forward compatibility. Always 0.
*/
public abstract void onLoadComplete(SoundPool soundPool, int soundId, int status); // implement yourself
/**
* Method to add sounds for which a test is required. Assumes that SoundPool.load(soundId,...) has been called.
*
* @param soundPool The SoundPool in which you loaded the sounds.
*/
public void addSound(int soundId) {
boolean isFirstOne;
synchronized (this) {
mySoundIds.add(soundId);
isFirstOne = (mySoundIds.size()==1);
}
if (isFirstOne) {
// first sound, start timer
testTimer = new Timer();
TimerTask task = new TimerTask() { // import java.util.TimerTask for this
@Override
public void run() {
testCompletions();
}
};
testTimer.scheduleAtFixedRate(task , 0, testPeriodMs);
}
}
private ArrayList<Integer> mySoundIds = new ArrayList<Integer>();
private Timer testTimer; // import java.util.Timer for this
private SoundPool testSoundPool;
private synchronized void testCompletions() {
ArrayList<Integer> completedOnes = new ArrayList<Integer>();
for (Integer soundId: mySoundIds) {
int streamId = testSoundPool.play(soundId, 0, 0, 0, 0, 1.0f);
if (streamId>0) { // successful
testSoundPool.stop(streamId);
onLoadComplete(testSoundPool, soundId, 0);
completedOnes.add(soundId);
}
}
mySoundIds.removeAll(completedOnes);
if (mySoundIds.size()==0) {
testTimer.cancel();
testTimer.purge();
}
}
}
좋은 질문, 나는 같은 문제 (http://stackoverflow.com/questions/3253108/how-했다 do-i-know-that-the-soundpool-is-ready-using-dd-target-below-2-2) 실제로 해결책을 찾지 못했습니다. – RoflcoptrException