この質問はこれに関連しています。
AudioManagerの代わりにSoundPoolを使用するようにコードを変更することができました。それが多かれ少なかれ機能することを知っています。
public class Sound {
private static boolean sound = true;
private static final int EAT_SOUND = 1;
private static final int SHORT_EAT_SOUND = 2;
private static final int EAT_CHERRY_SOUND = 3;
private static final int EAT_GHOST_SOUND = 4;
private static final int EXTRA_LIVE_SOUND = 5;
private static final int INTERMISSION_SOUND = 6;
private static final int OPENING_SOUND = 7;
private static final int PACMAN_DIES_SOUND = 8;
private static final int SIREN_SOUND = 9;
private static Context context;
private static SoundPool soundPool;
private static HashMap<Integer, Integer> soundPoolMap;
public static void initializeOpenSound(Context con) {
context = con;
soundPool = new SoundPool(2, AudioManager.STREAM_RING, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(OPENING_SOUND, soundPool.load(context, R.raw.opening_song, 1));
}
public static void initializeSounds() {
soundPoolMap.put(EAT_SOUND, soundPool.load(context, R.raw.eating, 1));
soundPoolMap.put(SHORT_EAT_SOUND, soundPool.load(context, R.raw.eating_short, 1));
soundPoolMap.put(EAT_CHERRY_SOUND, soundPool.load(context, R.raw.eating_cherry, 1));
soundPoolMap.put(EAT_GHOST_SOUND, soundPool.load(context, R.raw.eating_ghoasts, 1));
soundPoolMap.put(EXTRA_LIVE_SOUND, soundPool.load(context, R.raw.extra_lives, 1));
soundPoolMap.put(INTERMISSION_SOUND, soundPool.load(context, R.raw.intermission, 1));
soundPoolMap.put(PACMAN_DIES_SOUND, soundPool.load(context, R.raw.pac_man_dies, 1));
soundPoolMap.put(SIREN_SOUND, soundPool.load(context, R.raw.siren, 1));
}
private static void playSound(int sound) {
int streamID = soundPool.play(soundPoolMap.get(sound), 0.5f, 0.5f, 1, 0, 1f);
if (sound == OPENING_SOUND) {
soundPool.setLoop(streamID, 2);
}
}
public static void playSirenSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(SIREN_SOUND)) {
System.out.println("Play Siren sound");
playSound(SIREN_SOUND);
} else {
throw new SoundInitializationError("Siren Sound not initialized!");
}
}
}
public static void playPacmanDiesSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(PACMAN_DIES_SOUND)) {
System.out.println("Play Pacman Dies sound");
playSound(PACMAN_DIES_SOUND);
} else {
throw new SoundInitializationError("Pacman Dies Sound not initialized!");
}
}
}
public static void playOpeningSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(OPENING_SOUND)) {
System.out.println("Play Opening sound");
playSound(OPENING_SOUND);
} else {
throw new SoundInitializationError("Opening Sound not initialized!");
}
}
}
public static void playIntermissionSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(INTERMISSION_SOUND)) {
System.out.println("Play Intermission sound");
playSound(INTERMISSION_SOUND);
} else {
throw new SoundInitializationError("Intermission Sound not initialized!");
}
}
}
public static void playExtraLiveSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EXTRA_LIVE_SOUND)) {
System.out.println("Play Extra Live sound");
playSound(EXTRA_LIVE_SOUND);
} else {
throw new SoundInitializationError("Extra Live Sound not initialized!");
}
}
}
public static void playEatSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EAT_SOUND)) {
System.out.println("Play Eat Sound");
playSound(EAT_SOUND);
} else {
throw new SoundInitializationError("Eat Sound not initialized!");
}
}
}
public static void playShortEatSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(SHORT_EAT_SOUND)) {
System.out.println("Play Short Eat sound");
playSound(SHORT_EAT_SOUND);
} else {
throw new SoundInitializationError("Short Eat Sound not initialized!");
}
}
}
public static void playEatCherrySound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EAT_CHERRY_SOUND)) {
System.out.println("Play Eat Cherry sound");
playSound(EAT_CHERRY_SOUND);
} else {
throw new SoundInitializationError("Eat Cherry Sound not initialized!");
}
}
}
public static void playEatGhostSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EAT_GHOST_SOUND)) {
System.out.println("Play Eat Ghost sound");
playSound(EAT_GHOST_SOUND);
} else {
throw new SoundInitializationError("Eat Ghost Sound not initialized!");
}
}
}
public static boolean isSoundOn() {
return sound;
}
public static void setSoundOn(boolean b) {
sound = b;
}
}
しかし、起動時に再生したい多かれ少なかれ大きなオーディオファイルがあります。ただし、initializeOpenSound()を呼び出してからplayOpening Sound()を呼び出すと、サウンドは再生されず、logcatは次のように出力します。
07-15 09:11:02.121: WARN/SoundPool(14273): sample 1 not READY
SDK 2.2ではSoundPool.OnLoadCompleteListenerを使用しますが、SDK 2.1を使用して同じことを実現するにはどうすればよいですか?
編集:リスナーなどを含まないソリューションの幅も嬉しいですが、成功するまでサウンドファイルを再生してみてください。