我为我最后一年的项目创建了一个适用于 android 的音乐播放器。它具有播放、下一首、上一首等基本功能,还有一个用于从播放列表中选择歌曲的滚动条。
我想使用评分栏来允许用户设置歌曲的评分,但如果我为一首歌曲设置评分,然后从播放列表中选择另一首歌曲,那么也会为该歌曲设置相同的评分。
如何让它为不同的歌曲设置不同的评分?
我为我最后一年的项目创建了一个适用于 android 的音乐播放器。它具有播放、下一首、上一首等基本功能,还有一个用于从播放列表中选择歌曲的滚动条。
我想使用评分栏来允许用户设置歌曲的评分,但如果我为一首歌曲设置评分,然后从播放列表中选择另一首歌曲,那么也会为该歌曲设置相同的评分。
如何让它为不同的歌曲设置不同的评分?
歌曲更改时是否有回调?基本上,我们需要查看一些代码,但您需要将评级重置为 2 星,或者任何默认的星数。然后,您将需要使用OnRatingBarChange将评级保存到数据库中。似乎“MediaPlayer”类有一个OnCompletionListener来告诉轨道何时完成。
RatingBar ratingBar = (RatingBar)view.findViewById(R.id.ratingbar);
RatingBar.OnRatingBarChangeListener barListener =
new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
// Do your database stuff here
}
}
然后是 onCompletion 的内容
//mp is the reference to your media player
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// Do what you need to do to reset the rating bar. Might need to make a public reference or create a way for this inner class to access the rating bar.
}
});
OnClickListener
我认为,在活动的上一个和下一个歌曲发生变化时,将评分栏的速率设置为默认值。
ratingbar.setRating(3); //For three stars
并保存歌曲的速度OnRatingBarChange
。