Android 在Recyclerview中显示播放音乐项

Android 在Recyclerview中显示播放音乐项,android,android-music-player,Android,Android Music Player,我使用RecyclerView显示音乐列表。在每个项目中,我都有ImageView,其中显示了当前播放的音乐。我使用服务播放音乐。我的问题是,当一首音乐完成时,他开始播放下一首音乐,而在Recyclerview项中也必须显示在下一项中播放。我如何实现这一点 我试着使用listener。音乐在MusicService类重写方法中完成时 @Override public void onCompletion(MediaPlayer mp) { if(player.getCu

我使用RecyclerView显示音乐列表。在每个项目中,我都有ImageView,其中显示了当前播放的音乐。我使用服务播放音乐。我的问题是,当一首音乐完成时,他开始播放下一首音乐,而在Recyclerview项中也必须显示在下一项中播放。我如何实现这一点

我试着使用listener。音乐在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);
    }
音乐课

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;
}
私有上下文mContext;
私人回收站;
int选定位置=0;
int位置=0;
私有ArrayList歌曲=null;
公共静态歌曲选择;
共享引用sPref;
查看myItemView;
MusicService MusicService=新的MusicService();
公共KuilerAdapter(上下文mContext,数组列表歌曲,循环视图循环视图){
this.mContext=mContext;
this.recyclerView=recyclerView;
歌曲=歌曲;
sPref=mContext.getSharedReferences(“位置”,Context.MODE\u PRIVATE);
musicService.setTrackCompletedListener(此);
}
@凌驾
已完成的公共文件(){
notifyItemChanged(所选位置);
所选位置=所选位置+1;
notifyItemChanged(所选位置);
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
公共线性布局;
公共文本视图tvSongTitle;
公共文本视图描述;
公共图像视图imgEq;
公共MyViewHolder(视图){
超级(视图);
llSong=(LinearLayout)view.findviewbyd(R.id.llu-song);
tvSongTitle=(TextView)view.findViewById(R.id.song\u title);
tvSongDescription=(TextView)view.findViewById(R.id.song\u description);
imgEq=(ImageView)view.findViewById(R.id.play_eq);
}
}
@凌驾
公共MyViewHolder onCreateViewHolder(最终视图组父级,int-viewType){
最终视图itemView=LayoutInflater.from(parent.getContext())
.充气(R.layout.d\u media\u list\u项目,父项,false);
myItemView=itemView;
musicService.setMusicFolistener(本);
返回新的MyViewHolder(itemView);
}
@凌驾
公共无效onBindViewHolder(最终MyViewHolder,最终int位置){
Song currSong=songs.get(位置);
holder.tvSongTitle.setText(currSong.getTitle());
holder.tvSongDescription.setText(currSong.getArtist());
支架.llSong.setTag(位置);
sPref=mContext.getSharedReferences(“位置”,Context.MODE\u PRIVATE);
所选位置=sPref.getInt(“位置”,0);
如果(所选位置==位置){
AnimationDrawable动画=(AnimationDrawable)
ContextCompat.getDrawable(mContext,R.drawable.ic_均衡器_white_36dp);
DrawableCompat.setTintList(动画、ColorStateList.valueOf(ContextCompat.getColor(mContext、,
R.color.media_item_icon_playing));
holder.imgEq.setImageDrawable(动画);
holder.imgEq.setVisibility(视图可见);
animation.start();
}
否则{
holder.imgEq.setImageResource(R.drawable.ic\u play\u arrow\u black);
}
holder.itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Log.d(“pl”,“点击”);
notifyItemChanged(所选位置);
所选位置=位置;
notifyItemChanged(所选位置);
播放位置=位置;
songPick.songPick(holder.itemView);
AnimationDrawable动画=(AnimationDrawable)
ContextCompat.getDrawable(mContext,R.drawable.ic_均衡器_white_36dp);
DrawableCompat.setTintList(动画、ColorStateList.valueOf(ContextCompat.getColor(mContext、,
R.color.media_item_icon_playing));
holder.imgEq.setImageDrawable(动画);
holder.imgEq.setVisibility(视图可见);
animation.start();
SharedReferences.Editor=sPref.edit();
编辑器.putInt(“位置”,所选位置);
commit();
}
});
}
@凌驾
public int getItemCount(){
返回歌曲。大小();
}
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();


        }
    }
public static final String EXTRA\u CONNECTED\u CAST=“com.example.android.uamp.CAST\u NAME”;
私有字符串songTitle=“”;
私人弦乐歌手=”;
私有静态final int NOTIFY_ID=1;
私有布尔洗牌=false;
私有随机兰德;
//媒体播放器
私人媒体播放器;
//歌曲列表
私人雅乐歌曲;
//当前位置
私家车;
私有最终IBinder musicBind=新的MusicBinder();
公共静态变更信息变更信息;
公共静态轨道完工,轨道完工;
public void onCreate(){
//创建服务
super.onCreate();
//初始化位置
songPosn=0;
//创建玩家
player=新媒体播放器();
initMusicLayer();
//洗牌
rand=新随机数();
}
public void initMusicPlayer(){
//设置玩家属性
setWakeMode(getApplicationContext(),PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(此);
player.setOnCompletionListener(此);
player.setOneErrorListener(此);
}
公共无效集合列表(数组列表歌曲){
歌曲=歌曲;
}
公共空间集松(国际歌曲索引){
songPosn=songIndex;
}
公共类MusicBinder扩展活页夹{
公共音乐服务getService(){
返回MusicService.this;
}
}
@可空
@凌驾
公共IBinder onBind(意向){
返回音乐绑定;
}
@凌驾
公共布尔onUnbind(意图){
//player.stop();
//玩