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,706
  • 13
  • 57
  • 98
  • That is because of the buffersize. Maybe this could help you: http://stackoverflow.com/questions/4413300/change-buffer-size-on-mediaplayer – Patricia Oct 10 '15 at 21:02

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);
SuperFrog
  • 7,631
  • 9
  • 51
  • 81