Kotlin:RecyclerView with SoundPool:行点击时没有声音



单击RecyclerView行时,我没有播放任何声音。音单正在我的SecondAdapter中实现.SecondAdapter是我的RecyclerView活动类的适配器类。 当我单击行时,Logcat反映了这一点:

2019-12-01 18:57:21.340 24162-24162/com.shamuraq.svgintent6 W/SoundPool:   sample 1 not READY
2019-12-01 18:57:21.371 24162-24162/com.shamuraq.svgintent6 W/SoundPool:   sample 1 not READY
2019-12-01 18:57:21.392 24162-24256/com.shamuraq.svgintent6 I/OMXClient: IOmx service obtained
2019-12-01 18:57:21.431 24162-24162/com.shamuraq.svgintent6 W/SoundPool:   sample 1 not READY
2019-12-01 18:57:21.465 24162-24162/com.shamuraq.svgintent6 W/SoundPool:   sample 1 not READY
2019-12-01 18:57:21.497 24162-24162/com.shamuraq.svgintent6 W/SoundPool:   sample 1 not READY
2019-12-01 18:57:21.497 24162-24263/com.shamuraq.svgintent6 I/OMXClient: IOmx service obtained
2019-12-01 18:57:21.515 24162-24264/com.shamuraq.svgintent6 I/OMXClient: IOmx service obtained
2019-12-01 18:57:21.569 24162-24271/com.shamuraq.svgintent6 I/OMXClient: IOmx service obtained
2019-12-01 18:57:21.580 24162-24274/com.shamuraq.svgintent6 I/OMXClient: IOmx service obtained

可能是什么问题?

适配器类:

class SecondAdapter(val content:Array<String>) : RecyclerView.Adapter<SecondCustomViewGolder>(){
//var lessons = arrayOf("Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh",
//  "Lapan", "Sembilan")
var soundList = arrayOf(R.raw.ahem_x,R.raw.bad_disk_x,R.raw.baseball_hit,R.raw.bloop_x,R.raw.blurp_x)
override fun getItemCount(): Int {
return soundList.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SecondCustomViewGolder {
var layoutInflater = LayoutInflater.from(parent.context)
var cellForRow = layoutInflater.inflate(R.layout.lesson_row, parent, false)
return SecondCustomViewGolder(cellForRow)
}
override fun onBindViewHolder(holder: SecondCustomViewGolder, position: Int) {
holder.loadAndPlaySound(soundList.get(position), 1)
}
}
class SecondCustomViewGolder(var viewTwo : View) : RecyclerView.ViewHolder(viewTwo) {
private var soundEngine = SoundEngine()
fun loadAndPlaySound(soundIdToPlay:Int, priority: Int) {
val soundToPlay = soundEngine.load(viewTwo.context, soundIdToPlay, priority)
soundEngine.play(soundToPlay, 1F, 1F, 1, 0, 1F)
}
}

尝试更改adapter实现,如下所示:

class SecondAdapter(val content:Array<String>) : RecyclerView.Adapter<SecondCustomViewGolder>(){
//var lessons = arrayOf("Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh",
//  "Lapan", "Sembilan")
private var soundEngine = SoundEngine()
var soundList = arrayOf(R.raw.sound1, R.raw.sound2,R.raw.sound1, R.raw.sound2,
R.raw.sound1, R.raw.sound2,R.raw.sound1, R.raw.sound2,
R.raw.sound1)

override fun getItemCount(): Int {
return content.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SecondCustomViewGolder {
var layoutInflater = LayoutInflater.from(parent.context)
var cellForRow = layoutInflater.inflate(R.layout.lesson_row, parent, false)
return SecondCustomViewGolder(cellForRow)
}
override fun onBindViewHolder(holder: SecondCustomViewGolder, position: Int) {
holder.viewTwo.setOnClickListener {
val soundToPlay = soundEngine.load(holder.viewTwo.context, soundList.get(position), 1)
soundEngine.play(soundToPlay, 1F, 1F, 1, 0, 1F)
}
}
}
class SecondCustomViewGolder(var viewTwo : View) : RecyclerView.ViewHolder(viewTwo){
}

还要更改SoundEngine中的play实现,如下所示:

fun play(soundID: Int, leftVolume: Float, rightVolume: Float, priority: Int, loop: Int, rate: Float) {
soundPool.setOnLoadCompleteListener { soundPool, sampleId, _ ->
soundPool.play(sampleId, leftVolume, rightVolume, priority, loop, rate)
}
}

最新更新