2010-04-13 5 views
3

여러 사운드/비프 음을 동시에 연주하려면 코드를 다음과 같이 사용할 수 없습니다. 내 onclicklistener에서 추가했습니다 ... 공개 무효 onClick (보기 v) {mSoundManager.playSound (1); mSoundManager.playSound (2); } ... 그러나 이것은 한 번에 하나의 사운드만을 재생하며 인덱스 1의 사운드와 인덱스 2의 사운드를 재생합니다.Android에서 동시에 여러 사운드 재생

onClick() 이벤트가있을 때마다이 코드를 사용하여 최소한 2 개의 사운드를 동시에 재생하는 방법은 무엇입니까?

public class SoundManager { 

private SoundPool mSoundPool; 
private HashMap<Integer, Integer> mSoundPoolMap; 
private AudioManager mAudioManager; 
private Context mContext; 


public SoundManager() 
{ 

} 

public void initSounds(Context theContext) { 
    mContext = theContext; 
    mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
    mSoundPoolMap = new HashMap<Integer, Integer>(); 
    mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);   
} 

public void addSound(int Index,int SoundID) 
{ 
    mSoundPoolMap.put(1, mSoundPool.load(mContext, SoundID, 1)); 
} 

public void playSound(int index) { 

    int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
} 

public void playLoopedSound(int index) { 

    int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); 
} 

} 다음 클래스는 사운드 풀을 사용하여 사운드 FX에 출전 할 필요

+0

나는 당신이 당신의'mSoundPoolMap'에 인덱스와 같은 각각의 사운드를 저장하는 것 같아 ...'mSoundPoolMap.put (1, ...' –

+0

안녕 로마, 나는 위의 코드를 사용해야합니다, I mSoundPoolMap.put에 mSoundPoolMap 방법을 변경 (mSoundPool.load (mContext, SoundID, 인덱스 1)); 내가 사용하고 난이 this- 할 소리를 최종)와 SoundManager (사운드 mSoundManager = 새)와 SoundManager (사운드() 연주 할 때, \t을 \t mSoundManager.initSounds (getBaseContext()); \t \t mSoundManager.addSound (1, R.raw.1); \t \tmSou ndManager.addSound (2, R.raw.2); \t \t mSoundManager.addSound (3, R.raw.2); 그래서 인덱스 1,2와 3이 mSoundPoolMap.put()으로 이동한다는 것을 의미합니다. 그러나 그때조차 나는 동시에 소리를 낼 수 없다. 반갑습니다. – Wrapper

답변

6

:

public class SoundManager { 

    public static int SOUNDPOOLSND_MENU_BTN   = 0; 
    public static int SOUNDPOOLSND_WIN    = 1; 
    public static int SOUNDPOOLSND_LOOSE   = 2; 
    public static int SOUNDPOOLSND_DRAW    = 3; 
    public static int SOUNDPOOLSND_TICK1   = 4; 
    public static int SOUNDPOOLSND_TICK2   = 5; 
    public static int SOUNDPOOLSND_OUT_OF_TIME  = 6; 
    public static int SOUNDPOOLSND_HISCORE   = 7; 
    public static int SOUNDPOOLSND_CORRECT_LETTER = 8; 

    public static boolean isSoundTurnedOff; 

    private static SoundManager mSoundManager; 

    private SoundPool mSoundPool; 
    private SparseArray <Integer> mSoundPoolMap; 
    private AudioManager mAudioManager; 

    public static final int maxSounds = 4; 

    public static SoundManager getInstance(Context context) 
    { 
     if (mSoundManager == null){ 
      mSoundManager = new SoundManager(context); 
     } 

     return mSoundManager; 
    } 

    public SoundManager(Context mContext) 
    { 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); 
     mSoundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0); 

//  mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
//   public void onLoadComplete(SoundPool soundPool, int sampleId,int status) { 
//    loaded = true; 
//   } 
//  }); 

     mSoundPoolMap = new SparseArray<Integer>(); 
     mSoundPoolMap.put(SOUNDPOOLSND_MENU_BTN, mSoundPool.load(mContext, R.raw.menubutton, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_WIN, mSoundPool.load(mContext, R.raw.win, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_LOOSE, mSoundPool.load(mContext, R.raw.lose, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_TICK1, mSoundPool.load(mContext, R.raw.tick_0, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_TICK2, mSoundPool.load(mContext, R.raw.tick_1, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_OUT_OF_TIME, mSoundPool.load(mContext, R.raw.out_of_time, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_HISCORE, mSoundPool.load(mContext, R.raw.personal_highscore, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_CORRECT_LETTER, mSoundPool.load(mContext, R.raw.correct_letter, 1)); 

     // testing simultaneous playing 
     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(0), streamVolume, streamVolume, 1, 20, 1f); 
     mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 2, 1f); 
     mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f); 


    } 

    public void playSound(int index) { 
     if (isSoundTurnedOff) 
      return; 

     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
    } 

    public static void clear() 
    { 
     if (mSoundManager != null){ 
      mSoundManager.mSoundPool = null; 
      mSoundManager.mAudioManager = null; 
      mSoundManager.mSoundPoolMap = null; 
     } 
     mSoundManager = null; 
    } 
} 

이 동시에 코멘트 "동시 재생 테스트"아래에 언급 한 3 사운드를 재생 않습니다. 내 코드 리스너 (OnLoadCompleteListener)에서 주석을 구현해야 할 것 같습니다. 당신이 연주를 시작했을 때 소리가 들리지 않습니다. 언급 된 청취자를 구현하고 싶지 않다면 연주가 시작된 후 잠시 기다린 후 잠시 후 소리를 내기 시작하십시오.

+0

몇 초보다 긴 소리 나 1MB보다 큰 오디오 파일에는 SoundPool을 사용할 수 없습니다. – soshial