Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

android - Kotlin:带有SoundPool的RecyclerView:行无声音单击(Kotlin:RecyclerView with SoundPool: no sound on rows click)

I am not getting any sound played when I click the RecyclerView rows.

(单击RecyclerView行时,没有播放任何声音。)

The sound slips are being implemented in my SecondAdapter .

(声音SecondAdapter正在我的SecondAdapter中实现。)

The SecondAdapter is an adapter class for my RecyclerView Activity class.

(SecondAdapter是我的RecyclerView Activity类的适配器类。)

The Logcat reflected this when I click the rows:

(单击行时, 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

What could be the issue?

(可能是什么问题?)

Adapter class:

(适配器类:)

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)

    }
}
  ask by Joe Shamuraq translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try to change your adapter implementation like below:

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

}

Also change your play implementation in your SoundEngine like below:

(也可以如下更改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)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...