简体   繁体   中英

Android Mediaplayer stops playing the button click sound after i click the button multiple times

I am using Mediaplayer to play an mp3 sound of a button click , whenever i press the button it plays the sound . And it's working fine it's playing the sound but when i click it around 15 times it stops playing the sound i don't know if it's always around 15 or it's a random number of times. This is the code :

Note : You can ignore the when and if statments i don't think they have to do anything with this issue.

fun play_sound(which_one:Int){
    //Init MediaPlayer..
    val mediaPlayer: MediaPlayer? = MediaPlayer.create(this, R.raw.button_click)
    if(which_one == 1){
        val is_playing = mediaPlayer?.isPlaying
        when(is_playing){false-> mediaPlayer?.start()}
        }
}

And when i use debugging mode that's what i get :(I am using kotlin)

E/MediaPlayerNative: invoke failed: wrong state 0, mPlayer(0x7eb285d240)
E/MediaPlayer: Error (1,-19)
V/PlayerBase: baseStop() piid=85911
I/MediaPlayer: [HSM] stayAwake false uid: 10192, pid: 14428

That error is related to a run out of memory or codecs. The solution is : either you use a Mediaplayer pool or call release() method for each instance you finished use it.

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