2017-12-05 4 views
6

私は問題がある、私は同時に音を開始したい。正確な時間に3つ以上の短い音を鳴らすSoundPool(ピアノの和音)

私はループ(ピアノ音)で3-5の短い音を演奏します。最初の1ms、2番目の17msなどで遅れています。最後の音は60-90msです。

私はSoundPoolを使用しています。

誰もがこのような問題を抱えているか、この問題を解決できるライブラリを使用していた(複数の短い音を同期して開始する)のですか?以下は

例の試験サンプル(私はRxJavaを使用しますが、私はとRxJavaせずにそれをテストしている)である:あなたがリスナー(OnLoadCompleteListener)を実装する必要があるように思え

Observable.timer(150, TimeUnit.MILLISECONDS, Schedulers.single()) 
      .repeat() 
      .subscribe(aLong -> { 
       for (int soundId = 55; i < 60; i++) { 
        soundPool.play(soundId , 1f, 1f, 1, 0, 1); 
       } 
      }); 

答えて

0

SoundPoolは、ドキュメントに記載されているようにオーディオファイルを非同期に読み込むため、遅延が発生するのはこのためです。 here

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; 
    } 
} 
+0

サウンドがすでにロードされている3は、この実施例が見つかり

が鳴り、私はそれは多分スレッド/同期の問題 – Wrobel

+0

YAHだと思う、私は人々が二つのスレッドを使用して、ネット上でそのような解決策を見てみてください2つのサウンドのためにそれらを同期させるが、それは右に酔っていない – Rainmaker

+0

もう一つの方法ラウンドは、音を混ぜて1つとして再生することです、それはまた、一般的な解決策です – Rainmaker

関連する問題