2011-01-14 14 views
5

Su Android 2.2+ c'è una cosa chiamata SoundPool.OnLoadCompleteListener che permette di sapere se un suono è stato caricato con successo o meno.Sapere se il caricamento di un suono con Soundpool ha avuto successo su Android 1,6/2,0/2,1

Sto prendendo di mira versione API inferiore (idealmente 1.6 ma potrebbe andare per 2.1) e ho bisogno di sapere se un suono è stato caricato correttamente (come è stato selezionato dall'utente). Qual è il modo giusto per farlo?

spero non caricare il suono una volta con MediaPlayer e se corretto con Soundpool ?!

+0

Buona domanda, ho avuto lo stesso problema (http://stackoverflow.com/questions/3253108/how- do-i-know-che-il-Soundpool-è-pronto-uso-sdk-bersaglio-sotto-2-2) e non ha veramente trovato una soluzione. – RoflcoptrException

risposta

10

ho implementato un OnLoadCompleteListener classe di sorta-di-compatibile che funziona, almeno per Android 2.1.

Il costruttore prende un oggetto SoundPool e i suoni per i quali è stato chiamato il numero SoundPool.load(..) devono essere registrati con OnLoadCompleteListener.addSound(soundId). Dopo questo, l'ascoltatore tenta periodicamente di riprodurre i suoni richiesti (a volume zero). In caso di successo, chiama la tua implementazione di onLoadComplete, come nella versione Android 2.2+.

Ecco un esempio di utilizzo:

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. 

Ed ecco la fonte:

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

Soundpool fa caricare il file in modo asincrono. Purtroppo prima del livello API8 non c'è API per verificare se il caricamento è stato completato.

Come hai detto come di Android API8 è possibile verificare se il carico completo tramite un OnLoadCompleteListener. Ecco un piccolo esempio per questo: Android sounds tutorial.

Problemi correlati