I want to play a sound with a button. Here is my code (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
}
}
error code: None of the following functions can be called with the arguments supplied: public open fun create(p0: Context!, p1: Uri!): MediaPlayer! defined in android.media.MediaPlayer public open fun create(p0: Context!, p1: Int): MediaPlayer! defined in android.media.MediaPlayer
CodePudding user response:
I think the error is the this
param in the Mediaplayer create method, because the param must be a context but your this
is an adapter, so:
MediaPlayer.create(holder.itemView.context, R.raw.hello)