Android实现播放音乐列表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LosingCarryJie/article/details/79119694

首先我们看下QQ音乐,酷我音乐的列表图

这里写图片描述

这里写图片描述

网易云音乐也是这种列表,下面展示下网易云音乐的音乐列表gif图

这里写图片描述

总结下播放列表播放音乐的逻辑

1 点击某一首歌,就开始播放
2 当其他歌曲在播放的时候,点击新的一首歌,原先播放的暂停,新的歌曲播放
3 当前有一首歌在播放,即使列表滑出了屏幕当再次回到屏幕的时候还是显示这首歌在播放

下面开始撸代码:

package com.smartrefesh.listplayer;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smartrefesh.R;
import com.smartrefesh.model.MusicModel;

import java.util.List;

/**
 * Description  音乐播放列表的Adapter
 * Created by Administrator
 * Time 2018/1/21  15:13
 */

public class ListPlayerAdapter extends RecyclerView.Adapter<ListPlayerAdapter.MusicItemHolder> {
    private Context context;
    private List<MusicModel> list;
    public ListPlayerAdapter(Context context, List<MusicModel> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MusicItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MusicItemHolder(LayoutInflater.from(context).inflate(R.layout.item_text, parent, false));
    }

    @Override
    public void onBindViewHolder(MusicItemHolder holder, int position, List<Object> payloads) {
        if(payloads==null||payloads.size()==0){
            onBindViewHolder(holder, position);
        }else {
            holder.musicName.setTextColor(context.getResources().getColor(R.color.black));
        }
    }

    @Override
    public void onBindViewHolder(final MusicItemHolder holder, final int position) {
        final MusicModel musicModel = list.get(position);
        holder.musicName.setText(musicModel.getMusicName());
        int playPosition = MusicPlayer.getInstance().getLastPosition();//获取历史播放位置
        if(playPosition==position&&MusicPlayer.getInstance().isPlaying()){
                holder.musicName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        }else {
            holder.musicName.setTextColor(context.getResources().getColor(R.color.black));
        }

        holder.musicName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int playPosition = MusicPlayer.getInstance().getLastPosition();//点击切换更换了历史播放位置所以需要重新获取
                if(playPosition==position){
                    if(MusicPlayer.getInstance().isPlaying()){
                        MusicPlayer.getInstance().pause(position);
                        holder.musicName.setTextColor(context.getResources().getColor(R.color.black));
                    }else {
                        MusicPlayer.getInstance().playUrl(musicModel.getUrl(),position);
                        holder.musicName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    }
                }else {
                    notifyItemChanged(playPosition,"1");
                    MusicPlayer.getInstance().playUrl(musicModel.getUrl(),position);
                    holder.musicName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    static class MusicItemHolder extends RecyclerView.ViewHolder{
        private TextView musicName;
        public MusicItemHolder(View itemView) {
            super(itemView);
            musicName = itemView.findViewById(R.id.music_name);
        }
    }

}
public interface IPlayer {
    void playUrl(String url, int currentPosition);
    void pause(int currentPosition);
    boolean isPlaying();
    int getLastPosition();
}

最后看下效果图:

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页