2017-12-05 13 views
6

मुझे कोई समस्या है, मैं एक ही समय में ध्वनि शुरू करना चाहता हूं।सटीक समय में तीन या अधिक छोटी आवाज़ें चलाएं SoundPool (पियानो तार)

मैं लूप (पियानो ध्वनियों) में 3-5 छोटी आवाज बजाता हूं, और मुझे अंतिम ध्वनि पर 60-90ms तक, पहले 17ms पर, पहले 1ms पर देरी होती है, और इसी तरह।

मैं SoundPool का उपयोग कर रहा हूं।

किसी को भी इस तरह की समस्या है या पुस्तकालय का उपयोग किया है जो इस समस्या को हल कर सकता है (सिंक में कई छोटी आवाज़ें शुरू करें)?

नीचे उदाहरण परीक्षण नमूना (मैं RxJava का उपयोग, लेकिन मैं के साथ और RxJava बिना यह परीक्षण किया है) है:

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

ऐसा लगता है आपके श्रोता(OnLoadCompleteListener) को लागू करने की जरूरत है। SoundPool ऑडियो फ़ाइलों को अतुल्यकालिक रूप से लोड करता है क्योंकि यह दस्तावेज़ों में कहा गया है, इसलिए मेरा मानना ​​है कि आपको अपनी देरी क्यों मिलती है।

मिले 3 के लिए इस काम कर उदाहरण लगता 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

ध्वनि पहले से ही लोड किए गए हैं, मुझे लगता है कि शायद एक सूत्रण/सिंक्रनाइज़ समस्या है – Wrobel

+0

हाँ, मैं शुद्ध है कि लोगों को दो धागे का उपयोग करें और कोशिश पर इस तरह के एक समाधान देखा उन्हें दो ध्वनियों के लिए सिंक करने के लिए, लेकिन यह सही नहीं दिखता – Rainmaker

+0

दूसरी तरफ ध्वनि को मिश्रण करना और इसे एक के रूप में खेलना है, यह एक आम समाधान भी है – Rainmaker

संबंधित मुद्दे