0

I do not quite understand why there is a delay between the button click and sound.

Following is my code

    button = (Button) findViewById(R.id.playBtn);

    final MediaPlayer playButtonClick = MediaPlayer.create(this, R.raw.buttonsound);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            playButtonClick.start();

            Intent browserIntent =
                    new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
            startActivity(browserIntent);

        }
    }); 

Is there anyway I can reduce the delay?

blue piranha
  • 3,364
  • 12
  • 48
  • 86

1 Answers1

2

I think you should consider using SoundPool instead.

    SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
    HashMap<Integer, Integer> soundPoolMap soundPoolMap = new HashMap<Integer, Integer>();
    soundPoolMap.put(soundID, soundPool.load(this, R.raw.your_sound, 1));

And then you can play the sound using:

soundPool.play(soundId, 1, 1, 1, 0, 0);
Udi Idan
  • 7,574
  • 9
  • 49
  • 81