kotlin RecyclerView 播放声音

kotlin RecyclerView play sound

我想用按钮播放声音。 这是我的代码 (adapter.kt)

package com.example.koszono

import android.content.Intent
import android.media.MediaPlayer
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.recycler_row.view.*

class Adapter (val koszones: ArrayList<String>,
            val hangList : ArrayList<Int>): RecyclerView.Adapter<Adapter.KoszonesVH>()       {
class KoszonesVH (itemView: View): RecyclerView.ViewHolder(itemView){


}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KoszonesVH {
    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.recycler_row, parent, false)
    return KoszonesVH(itemView)
}

override fun onBindViewHolder(holder: KoszonesVH, position: Int) {
    holder.itemView.recycler_textView.text = koszones.get(position)


    var mMediaPlayer: MediaPlayer? = null
    holder.itemView.recycler_play.setOnClickListener {

        if (mMediaPlayer == null) {
            mMediaPlayer = MediaPlayer.create(this, R.raw.hello)
            mMediaPlayer!!.isLooping = false
            mMediaPlayer!!.start()
        } else mMediaPlayer!!.start()



    }







    holder.itemView.setOnClickListener {
        val intent = Intent (holder.itemView.context, activity_tartalom::class.java)
        intent.putExtra("koszones", koszones.get(position))
        intent.putExtra("hang_intent", hangList.get(position))
        holder.itemView.context.startActivity(intent)
    }
}

override fun getItemCount(): Int {
    return koszones.size
}
}

错误代码:None 可以使用提供的参数调用以下函数: public open fun create(p0: Context!, p1: Uri!): MediaPlayer!在 android.media.MediaPlayer 中定义 public open fun create(p0: Context!, p1: Int): MediaPlayer!在 android.media.MediaPlayer

中定义

我认为错误是 Mediaplayer create 方法中的 this 参数,因为该参数必须是上下文,但您的 this 是适配器,所以:

MediaPlayer.create(holder.itemView.context, R.raw.hello)