In the raw directory I have files named rhode1.mp3 to rhode49.mp3.
I need to randomly play one of these files so I'm randomly generating a number and attaching it to R.raw.rhode
to use as URI (uri1
in the code below), but it crashes the app. When I replace uri1
with R.raw.rhode1
for example it works and the audio file plays.
I tried writing R.raw.rhode$note1.mp3
and it still didn't work. Also tried:
("R.raw.rhode" + note1.toString() + ".mp3")
And:
("R.raw.rhode" + note1.toString())
override fun onCreate (savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var mediaplayer: MediaPlayer? = null
val letsgo = findViewById<Button>(R.id.start)
val think = findViewById<SeekBar>(R.id.think)
val extime = findViewById<SeekBar>(R.id.extime)
val rpt = findViewById<SeekBar>(R.id.rpt)
val rtime = findViewById<SeekBar>(R.id.rtime)
var note1: Int
var uri1: Uri
letsgo.setOnClickListener {
note1 = Random.nextInt(49) + 1
uri1 = Uri.parse("R.raw.rhode" + note1.toString())
mediaplayer = MediaPlayer.create(applicationContext, uri1)
mediaplayer!!.start()
}
}