开发者

Pre-loading sound effects

I have the counter working (thanks for the help) and I have added sound using MediaPlayer, the problem is that there is a delay loading the counter because the sounds are loaded on the same class.

Is there a way to pre-load the sounds using the main class, so then, when the counter is called, the numbers are buffered and can be called from this class?

The application has a splash screen and the only purpose is to load anything needed by the other classes to avoid delays.

If sounds can't be loaded from another class, could they be loaded individually at play time?

This is what I have so far...

final boolean sound = timer_sound.getBoolean("timer_sound", true);

  final MediaPlayer number01 = MediaPlayer.create(this, R.raw.number_1);
  final MediaPlayer number02 = MediaPlayer.create(this, R.raw.number_2);
  final MediaPlayer number03 = MediaPlayer.create(this, R.raw.number_3);
  final MediaPlayer number04 = MediaPlayer.create(this, R.raw.number_4);
  final MediaPlayer number05 = MediaPlayer.create(this, R.raw.number_5);
  final MediaPlayer number06 = MediaPlayer.create(this, R.raw.number_6);
  final MediaPlayer number07 = MediaPlayer.create(this, R.raw.number_7);
  final MediaPlayer number08 = MediaPlayer.create(this, R.raw.number_8);
  final MediaPlayer number09 = MediaPlayer.create(this, R.raw.number_9);
  final MediaPlayer number10 = MediaPlayer.create(this, R.raw.number_10);
  final MediaPlayer number15 = MediaPlayer.create(this, R.raw.number_15);
  final MediaPlayer number20 = MediaPlayer.create(this, R.raw.number_20);

lastSeconds = (TextView) findViewById(R.id.lastminuteseconds);

lastMinute = new CountDownTimer(60 * 1000, 1000) {

  public void onTick(long secondsToStart) {
    int elapseTime = (int) (secondsToStart / 1000);

    if (sound == true){
 开发者_高级运维     switch(elapseTime) {
        case 1: number01.start();break;
        case 2: number02.start();break;
        case 3: number03.start();break;
        case 4: number04.start();break;
        case 5: number05.start();break;
        case 6: number06.start();break;
        case 7: number07.start();break;
        case 8: number08.start();break;
        case 9: number09.start();break;
        case 10: number10.start();break;
        case 15: number15.start();break;
        case 20: number20.start();break;
      }
    }

this is working almost fine, the problem is that there is a 2 second delay loading the files, and I wanted to add few more (50, 40, 30).


Your approach seems very wasteful. You should load the first sound, and upon playing it trigger the load of the next sound, so at any time you are only caching two sounds (current and next) in RAM.

So you pre-load only first sound, then in ontick() you trigger load of next sound upon playing current sound via run() and swap the sound objects for next ontick().


I am learning slowly and I have figured this out.

I can't believe how simple it was to do this....

Not to put all the code, here are the changes done;

First, I stop loading the sound with the switch, but I used the switch call a class..

  if (sound == true){
    switch(elapseTime) {
      case 1: number01();break;
      case 2: number02();break;

Then, I load the sounds when the class is called;

 public void number01(){
   final MediaPlayer number01 = MediaPlayer.create(this, R.raw.number_1);
     number01.start();
   }
 public void number02(){
    final MediaPlayer number02 = MediaPlayer.create(this, R.raw.number_2);
    number02.start();
 }

Now when the switch is called and it loads the sound and plays it. This stops the delay when loading the application.

I hope this helps other beginners, that like me, are learning by making mistakes and correcting them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜