简体   繁体   中英

Android MediaPlayer or SoundPool?

When I use mediaplayer on click button, after several clicks the sound disappears. When I use soundpool, the time is too short to play the effect sound. Can anybody please help me with this?

As I understood your audio is 20 s. So you can use this code on your button click:

if (mediaPlayer != null){
    mediaPlayer.stop();
    mediaPlayer.release();
    mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();

The first if statement is to take care that it will keep playing how much ever you clicked the button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM