I want to play mp3 file from res/raw folder when user click on button
my code is below:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
MediaPlayer mp = MediaPlayer.create(MainActivity.this,
R.raw.click);
mp.start();
}
});
Work well but take some time to play after button click. please, anyone, give me a solution. Thanks
Option 1:
Instead of creating the media player and then calling start() method in the onClick of the button, you can initialize the mediaPlayer in onCreate() of activity.
As for the button click implementation, you need to call mediaPlayer.start() only.
Option 2: You can use SoundPool API provided by Android. This has lower latency compared to MediaPlayer API. SoundPool are recommended mainly for playing short clips.