I want to load more than one sound source file in kotlin soundPool.

Asked 2 years ago, Updated 2 years ago, 115 views

Using the following article as a reference, I am creating an Android app on kotlin.

https://akira-watson.com/android/kotlin/soundpool-play.html

What I want to do is create about 50 buttons and make different sounds from each button in the sound pool.
I think I can write briefly if I can write in a loop sentence, but when I load the sound source,

soundOne=soundPool.load(this, R.raw.one, 1)

Would it be possible to use R.raw.one in as a variable and substitute the name of the sound source?

Also, button on-click events

button1.setOnClickListener

Can I use button1 in as a variable and turn it into a for statement?

android kotlin

2022-09-30 17:09

1 Answers

Name the audio file e.g. "sound1.mp3", "sound2.mp3", ... "sound50.mp3", and
Why don't you get the second argument for soundPool.load, as follows?

 for (i in 1..50) {
    val resourceId = this.resources.getIdentifier("sound$i", "raw", this.packageName)

}


2022-09-30 17:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.