I'm trying to create a button that plays sound using SoundPool. The sound is created in another class and i'm trying to access it from my MainActivity as follow:
MainActivity:
class MainActivity : AppCompatActivity() {
private var soundPool: SoundPool? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sound1 = SoundEngine().load(this, R.raw.ahem_x, 1)//<-error pop up at load
button1.setOnClickListener { soundPool!!.play(sound1, 1f, 1f, 1, 0, 1f) }
}
}
Sound class:
class SoundEngine() {
private var soundPool: SoundPool? = null
fun someCode():Int{
soundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_GAME)
.build()
SoundPool.Builder()
.setMaxStreams(1)
.setAudioAttributes(audioAttributes)
.build()
} else {
SoundPool(1, AudioManager.STREAM_MUSIC, 0)
}
val sound1 = soundPool!!.load(MainActivity(), R.raw.ahem_x, 1)
return sound1
}
}
I'm getting an error at val sound1 = SoundEngine().load(this, R.raw.ahem_x, 1) in MainActivity with pop up error Unresolved reference load. I googled about this and was overwhelmed by the information and got further confused. Please point me in the right direction. Thank in advance.
SoundEngineis calledsomeCodeand you're trying to call it asload?load, you have to use that name at its declaration as well, instead ofsomeCode.