0

RecyclerView单击行时,我没有播放任何声音。声音单据正在我的SecondAdapter. 这SecondAdapter是我的RecyclerViewActivity 类的适配器类。当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)

    }
}
4

1 回答 1

1

尝试更改您的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){

}

还可以在下面更改您的play实现SoundEngine

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)
    }
}
于 2019-12-01T12:24:07.433 回答