我使用RecyclerView显示音乐列表。在每个项目中我都有ImageView,它显示当前播放的音乐。我用服务播放音乐。我的问题是当一首音乐完成后,他开始播放下一首音乐,而在Recyclerview项目中也必须显示下一个音乐。我该如何实现呢。
我尝试使用监听器。音乐在MusicService类中完成覆盖方法
@Override
public void onCompletion(MediaPlayer mp) {
if(player.getCurrentPosition()>0){
mp.reset();
playNext();
trackCompleted.letTrackCompleted();
changeInfo.setTrackInfo();
}
}
当这个方法出现时,我尝试让音乐完成的知道适配器。 并手动尝试更改播放项目的图标
@Override
public void letTrackCompleted() {
notifyItemChanged(selected_position);
selected_position= selected_position+1;
notifyItemChanged(selected_position);
}
MusicAdapter.class
private Context mContext;
private RecyclerView recyclerView;
int selected_position = 0;
int playing_position = 0;
private ArrayList<Song> songs = null;
public static SongPick songPick;
SharedPreferences sPref;
View myItemView;
MusicService musicService = new MusicService();
public KuilerAdapter(Context mContext, ArrayList<Song> theSongs, RecyclerView recyclerView) {
this.mContext = mContext;
this.recyclerView = recyclerView;
songs=theSongs;
sPref = mContext.getSharedPreferences("position",Context.MODE_PRIVATE);
musicService.setTrackCompletedListener(this);
}
@Override
public void letTrackCompleted() {
notifyItemChanged(selected_position);
selected_position= selected_position+1;
notifyItemChanged(selected_position);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public LinearLayout llSong;
public TextView tvSongTitle;
public TextView tvSongDescription;
public ImageView imgEq;
public MyViewHolder(View view) {
super(view);
llSong = (LinearLayout) view.findViewById(R.id.ll_song);
tvSongTitle = (TextView)view.findViewById(R.id.song_title);
tvSongDescription = (TextView)view.findViewById(R.id.song_description);
imgEq = (ImageView) view.findViewById(R.id.play_eq);
}
}
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.d_media_list_item, parent, false);
myItemView = itemView;
musicService.setMusicInfoListener(this);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Song currSong = songs.get(position);
holder.tvSongTitle.setText(currSong.getTitle());
holder.tvSongDescription.setText(currSong.getArtist());
holder.llSong.setTag(position);
sPref = mContext.getSharedPreferences("position", Context.MODE_PRIVATE);
selected_position = sPref.getInt("position", 0);
if(selected_position == position){
AnimationDrawable animation = (AnimationDrawable)
ContextCompat.getDrawable(mContext, R.drawable.ic_equalizer_white_36dp);
DrawableCompat.setTintList(animation, ColorStateList.valueOf(ContextCompat.getColor(mContext,
R.color.media_item_icon_playing)));
holder.imgEq.setImageDrawable(animation);
holder.imgEq.setVisibility(View.VISIBLE);
animation.start();
}
else{
holder.imgEq.setImageResource(R.drawable.ic_play_arrow_black);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("pl", "Click");
notifyItemChanged(selected_position);
selected_position = position;
notifyItemChanged(selected_position);
playing_position = position;
songPick.songPicked(holder.itemView);
AnimationDrawable animation = (AnimationDrawable)
ContextCompat.getDrawable(mContext, R.drawable.ic_equalizer_white_36dp);
DrawableCompat.setTintList(animation, ColorStateList.valueOf(ContextCompat.getColor(mContext,
R.color.media_item_icon_playing)));
holder.imgEq.setImageDrawable(animation);
holder.imgEq.setVisibility(View.VISIBLE);
animation.start();
SharedPreferences.Editor editor = sPref.edit();
editor.putInt("position", selected_position);
editor.commit();
}
});
}
@Override
public int getItemCount() {
return songs.size();
}
void setSongPickedListener(SongPick sp){
songPick = sp;
}
}
MusicService.java
public static final String EXTRA_CONNECTED_CAST = "com.example.android.uamp.CAST_NAME";
private String songTitle="";
private String songArtist = "";
private static final int NOTIFY_ID=1;
private boolean shuffle=false;
private Random rand;
//media player
private MediaPlayer player;
//song list
private ArrayList<Song> songs;
//current position
private int songPosn;
private final IBinder musicBind = new MusicBinder();
public static ChangeInfo changeInfo;
public static TrackCompleted trackCompleted;
public void onCreate(){
//create the service
super.onCreate();
//initialize position
songPosn=0;
//create player
player = new MediaPlayer();
initMusicPlayer();
//for shuffle
rand=new Random();
}
public void initMusicPlayer(){
//set player properties
player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}
public void setList(ArrayList<Song> theSongs){
songs=theSongs;
}
public void setSong(int songIndex){
songPosn=songIndex;
}
public class MusicBinder extends Binder {
public MusicService getService() {
return MusicService.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return musicBind;
}
@Override
public boolean onUnbind(Intent intent){
// player.stop();
// player.release();
return false;
}
public void playSong(){
//play a song
player.reset();
//get song
Song playSong = songs.get(songPosn);
songTitle=playSong.getTitle();
songArtist = playSong.getArtist();
//get id
long currSong = playSong.getID();
//set uri
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
currSong);
try{
player.setDataSource(getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
player.prepareAsync();
}
@Override
public void onDestroy() {
stopForeground(true);
}
@Override
public void onCompletion(MediaPlayer mp) {
if(player.getCurrentPosition()>0){
mp.reset();
playNext();
trackCompleted.letTrackCompleted();
changeInfo.setTrackInfo();
}
}