I'm trying to create a button that plays sound using SoundPool.
(我正在尝试创建一个使用SoundPool播放声音的按钮。)
The sound is created in another class and i'm trying to access it from my MainActivity as follow: (声音是在另一个类中创建的,我正尝试通过MainActivity访问它,如下所示:)
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
.
(我在MainActivity中的val sound1 = SoundEngine().load(this, R.raw.ahem_x, 1)
出现错误,弹出错误Unresolved reference load
。)
I googled about this and was overwhelmed by the information and got further confused. (我在Google上搜索了一下,但对这些信息不知所措,进一步感到困惑。)
Please point me in the right direction. (请指出正确的方向。)
Thank in advance. (预先感谢。)
ask by Joe Shamuraq translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…