2010-07-15 11 views
2

이 질문은 this one과 관련 있습니다.2.2 이하의 SDK 대상을 사용하여 Soundpool을 사용할 준비가되었는지 어떻게 알 수 있습니까?

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 사운드(), 다음 소리가 재생되지 않고 로그 캣 출력을 호출하는 경우 :

07-15 09:11:02.121: WARN/SoundPool(14273): sample 1 not READY 

SDK 2.2에서 내가 SoundPool.OnLoadCompleteListener을 사용하지만 어떻게 내가 SDK 2.1을 사용하여 동일한 달성 할 수 있습니까?

편집 : 나는 또한 청취자 또는 그와 비슷한 것을 포함하지 않는 해결책을 찾았지만 성공할 때까지 사운드 파일을 재생하려고합니다.

답변

1

사운드를 초기화 한 후 바로 재생하려고하면 읽었을 때 준비가되지 않고이 오류가 발생합니다. 로딩 페이지를 만들고 사운드가로드 될 때까지 몇 초 동안 기다릴 수 있습니다.

또한 파일 크기가 1048576 바이트를 넘지 않도록하십시오. 이는 사운드 풀의 제한 사항입니다. 배경 음악이나 무언가를 연주하고 싶다면 미디어 플레이어를 사용해야합니다.

+0

예, 몇 초 기다리는 것도 제 생각이었습니다. 하지만 1 초 10 초 시도했지만 아무것도 작동하지 .... – RoflcoptrException

1

나는 here을 게시 한 OnLoadCompleteListener에 대한 대체 구현을 구현했습니다.