Delay while playing 2 sounds simultaneously in the timer

I have a small application that basically sets up a timer and plays sets of 2 sounds one by one .

I tried 2 timers because I wanted both sounds to start exactly at the same time every time. I gave a 500ms application to set both timers before they start

    Calendar cal = Calendar.getInstance();
    Date start = new Date(cal.getTime().getTime() + 500);

    timerTask1 = new TimerTask() { //1st timer
      @Override
      public void run() {
          soundManager.playSound(1);
      }
    };
    timer1 = new Timer();
    timer1.schedule(timerTask1, start, 550);


    timerTask2 = new TimerTask() { //2nd timer
      @Override
      public void run() {
          soundManager.playSound(2);
      }
    };
    timer2 = new Timer();
    timer2.schedule(timerTask2, start, 550);
}

soundManager is a SoundManager object based on this guide . The only change I made was to decrease the number of available streams from 20 to 2, as I only play 2 sounds at a time.

. Motorola RAZR, . , , . . ?

OGG

EDIT: . 2 . . 500 .

  • aproach TimerTask - . 300 , , (2 ) 497 , , .
  • 2- aproach AsyncTask - 475 500 , , TimerTask,

aproach .

+5
2

, , SoundPool:

public class Sound {
    SoundPool soundPool;
    int soundID;

    public Sound(SoundPool soundPool, int soundID) {
        this.soundPool = soundPool;
        this.soundID = soundID;
    }

    public void play(float volume) {
        soundPool.play(soundID, volume, volume, 0, 0, 1);
    }

    public void dispose() {
        soundPool.unload(soundID);
    }
}

, - :

SoundPool soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
AssetFileDescriptor assetFileDescriptor = activity.getAssets().openFd(fileName);
int soundID = soundPool.load(assetFileDescriptor, 0);

Sound sound = new Sound(soundPool, soundID);

:

sound.play(volume);

P.S. , , .

+6

SoundPool? , MediaPlayer. - .

+1

All Articles