당신이 가진 각각의 사운드 파일을 표현하기 위해 아래의 클래스를 사용하여 감사드립니다. 필요에 따라 주위에 보관하고 메모리 누출을 피하기 위해 처리가 끝난 후 처분하십시오.
public class AndroidSound implements Sound {
int soundId;
SoundPool soundPool;
public AndroidSound(SoundPool soundPool, int soundId) {
this.soundId = soundId;
this.soundPool = soundPool;
}
@Override
public void play(float volume) {
soundPool.play(soundId, volume, volume, 0, 0, 1);
}
@Override
public void dispose() {
soundPool.unload(soundId);
}
} 클래스 'newSound 방법 아래
를 사용하여 당신이 원하는 때마다 재생하고 처분 할 수있는 새로운 사운드 인스턴스를 얻을. 사운드를 만들어 콜렉션에 저장하고 필요할 때 사용할 수 있습니다.
public class AndroidAudio implements Audio {
AssetManager assets;
SoundPool soundPool;
public AndroidAudio(Activity activity) {
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
this.assets = activity.getAssets();
this.soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
}
@Override
public Music newMusic(String filename) {
try {
AssetFileDescriptor assetDescriptor = assets.openFd(filename);
return new AndroidMusic(assetDescriptor);
} catch (IOException e) {
throw new RuntimeException("Couldn't load music '" + filename + "'");
}
}
@Override
public Sound newSound(String filename) {
try {
AssetFileDescriptor assetDescriptor = assets.openFd(filename);
int soundId = soundPool.load(assetDescriptor, 0);
return new AndroidSound(soundPool, soundId);
} catch (IOException e) {
throw new RuntimeException("Couldn't load sound '" + filename + "'");
}
}
출처
2013-07-12 15:26:19
neo
감사합니다, 나는 당신의 접근 방식을 시도 할 것이다. 여러 객체에 대해 설정하려고하면됩니다. –