2011-01-14 5 views
5

सफल रहा है एंड्रॉयड पर 2.2+ कुछ SoundPool.OnLoadCompleteListener कहा जाता है जानने के लिए कि एक ध्वनि सफलतापूर्वक या नहीं लोड किया गया है की अनुमति देता है वहाँ है।यह जानते हुए कि अगर SoundPool साथ एक ध्वनि की लोडिंग एंड्रॉयड 1.6 पर/2.0/2.1

मैं कम API वर्शन लक्षित कर रहा हूं (आदर्श 1.6 लेकिन 2.1 के लिए जा सकते हैं) और मुझे पता है कि एक ध्वनि सही ढंग से लोड किया गया है कि क्या (के रूप में यह उपयोगकर्ता द्वारा चुना जाता है) की जरूरत है। ऐसा करने का सही तरीका क्या है?

मैं एक बार MediaPlayer साथ और SoundPool साथ सही है, तो ध्वनि लोड नहीं उम्मीद ?!

+0

अच्छा सवाल है, मैं एक ही समस्या (http://stackoverflow.com/questions/3253108/how- था डू-आई-जान-द-द-साउंडपूल-तैयार-उपयोग-एसडीके-लक्ष्य-नीचे -2-2) और वास्तव में कोई समाधान नहीं मिला। – RoflcoptrException

उत्तर

10

मैं एक प्रकार के- संगत OnLoadCompleteListener वर्ग कि एंड्रॉयड 2.1 के लिए कम से कम काम करता है को लागू किया।

कन्स्ट्रक्टर SoundPool ऑब्जेक्ट लेता है, और लगता है कि SoundPool.load(..) को कॉल किया गया है OnLoadCompleteListener.addSound(soundId) के साथ पंजीकृत होना चाहिए। इसके बाद, श्रोता समय-समय पर अनुरोधित ध्वनियां (शून्य मात्रा पर) चलाने का प्रयास करता है। यदि सफल हो, तो यह आपके 2.212 संस्करण में onLoadComplete के कार्यान्वयन को कॉल करता है।

यहाँ एक उपयोग उदाहरण है:

SoundPool mySoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
    OnLoadCompleteListener completionListener = new OnLoadCompleteListener(mySoundPool) { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int soundId, int status) { 
      Log.i("OnLoadCompleteListener","Sound "+soundId+" loaded."); 
     } 
    } 
    int soundId=mySoundPool.load(this, R.raw.funnyvoice,1); 
    completionListener.addSound(soundId); // tell the listener to test for this sound. 

और यहाँ स्रोत है:

abstract class OnLoadCompleteListener {  
    final int testPeriodMs = 100; // period between tests in ms 

    /** 
    * OnLoadCompleteListener fallback implementation for Android versions before 2.2. 
    * After using: int soundId=SoundPool.load(..), call OnLoadCompleteListener.listenFor(soundId) 
    * to periodically test sound load completion. If a sound is playable, onLoadComplete is called. 
    * 
    * @param soundPool The SoundPool in which you loaded the sounds. 
    */ 
    public OnLoadCompleteListener(SoundPool soundPool) { 
     testSoundPool = soundPool; 
    } 

    /** 
    * Method called when determined that a soundpool sound has been loaded. 
    * 
    * @param soundPool The soundpool that was given to the constructor of this OnLoadCompleteListener 
    * @param soundId The soundId of the sound that loaded 
    * @param status  Status value for forward compatibility. Always 0. 
    */ 
    public abstract void onLoadComplete(SoundPool soundPool, int soundId, int status); // implement yourself 

    /** 
    * Method to add sounds for which a test is required. Assumes that SoundPool.load(soundId,...) has been called. 
    * 
    * @param soundPool The SoundPool in which you loaded the sounds. 
    */ 
    public void addSound(int soundId) { 
     boolean isFirstOne; 
     synchronized (this) { 
      mySoundIds.add(soundId); 
      isFirstOne = (mySoundIds.size()==1); 
     } 
     if (isFirstOne) { 
      // first sound, start timer 
      testTimer = new Timer(); 
      TimerTask task = new TimerTask() { // import java.util.TimerTask for this 
       @Override 
       public void run() { 
        testCompletions(); 
       } 
      }; 
      testTimer.scheduleAtFixedRate(task , 0, testPeriodMs); 
     } 
    } 

    private ArrayList<Integer> mySoundIds = new ArrayList<Integer>(); 
    private Timer testTimer; // import java.util.Timer for this 
    private SoundPool testSoundPool; 

    private synchronized void testCompletions() { 
     ArrayList<Integer> completedOnes = new ArrayList<Integer>(); 
     for (Integer soundId: mySoundIds) { 
      int streamId = testSoundPool.play(soundId, 0, 0, 0, 0, 1.0f); 
      if (streamId>0) {     // successful 
       testSoundPool.stop(streamId); 
       onLoadComplete(testSoundPool, soundId, 0); 
       completedOnes.add(soundId); 
      } 
     } 
     mySoundIds.removeAll(completedOnes); 
     if (mySoundIds.size()==0) { 
      testTimer.cancel(); 
      testTimer.purge(); 
     } 
    } 
} 
1

ध्वनिपूल फ़ाइल को असीमित रूप से लोड करता है। एपीआई 8 स्तर से पहले दुर्भाग्य से कोई एपीआई जांचने के लिए नहीं है कि लोडिंग पूरी तरह से हो रही है या नहीं।

आप ने कहा के रूप में एंड्रॉयड API8 के रूप में यह जांच करने के लिए संभव है अगर लोड हो रहा है एक OnLoadCompleteListener के माध्यम से पूरा हो गया। इसके लिए यहां एक छोटा सा उदाहरण दिया गया है: Android sounds tutorial

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