how should i add sound in android studio which plays on button click

470 Views Asked by At

this is my code

fun onclick(view: View) {
    val numberRandom = (1..6).random()
    textView1.text = "$numberRandom"

    var mediaPlayer: MediaPlayer? = MediaPlayer.create(this, R.raw.rolll.wav)
    mediaPlayer?.start() // no need to call prepare(); create() does that for you


}
2

There are 2 best solutions below

3
Filip Sollár On

Don't include .wav extension in your resource, it should be

R.raw.rolll
2
AudioBubble On

You should pass R.raw.rolll not extension with it like below

MediaPlayer.create(this, R.raw.rolll)

with regrads

xitij