我正在构建一个用于播放音乐的按钮,但有一个问题,我正在使用 recyclerview 构建这个项目,我希望如果我按下音乐 1 的按钮,然后按下音乐 2 的按钮,则音乐 1 是停了下来。这就是我尝试的
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = dataset[position]
var buton = false
holder.textview.text = context.resources.getString(item.stringid)
holder.quotes.text = context.resources.getString(item.quotesid)
holder.namalagu.text = context.resources.getString(item.laguid)
holder.button.setOnClickListener {
if (!holder.lagu.isPlaying && !buton){
holder.lagu = MediaPlayer.create(context, item.fileLaguid)
holder.lagu.start()
holder.button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.pause, 0, 0, 0)
holder.button.text = context.resources.getString(R.string.pause)
buton = true
}
else if (holder.lagu.isPlaying && !buton){ <- this is the part where i try implement those feature
holder.lagu.stop()
holder.button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.play, 0, 0, 0)
holder.button.text = context.resources.getString(R.string.play)
buton = true
}
else{
holder.lagu.pause()
holder.button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.play, 0, 0, 0)
holder.button.text = context.resources.getString(R.string.play)
buton = false
}
}
我尝试从谷歌搜索,但仍然没有答案,我感谢你的帮助
您可以使用一个
MediaPlayer
作为您的列表。
但您需要跟踪玩家的状态。
单击列表中任意位置的按钮时,您首先会停止播放器(如果当前正在播放某些内容)。接下来,您将重置播放器并设置新的数据源。最后,您准备并启动播放器。
最后不要忘记释放播放器。