Android 超简单音乐播放器(二)添加本地音乐 (RecyclerView)(Cursor)

扫描本地音乐

关于 Cursor

Cursor 是每行的集合。
使用 moveToFirst() 定位第一行。 你必须知道每一列的名称。 你必须知道每一列的数据类型。
Cursor 是一个随机的数据源。 所有的数据都是通过下标取得。 关于 Cursor 的重要方法:

  1. close() 关闭游标,释放资源
  2. copyStringToBuffer(int columnIndex,CharArrayBuffer buffer) 在缓冲区中检索请求的列的文本,将将其存储
  3. getColumnCount()返回所有列的总数
  4. getColumnIndex(String columnName) 返回指定列的名称,如果不存在返回-1
  5. getColumnIndexOrThrow(String columnName) 从零开始返回指定列名称,如果不存在将抛出IllegalArgumentException 异常。
  6. getColumnName(intcolumnIndex) 从给定的索引返回列名
  7. getColumnNames() 返回一个字符串数组的列名
  8. getCount()返回Cursor 中的行数
  9. moveToFirst() 移动光标到第一行
  10. moveToPosition(int position) 移动光标到一个绝对的位置
 public List<Song> getLocalMusci(Context context) {
        List<Song> list1 = new ArrayList<Song>();
        int position = 0;
        Cursor cursor = context.getContentResolver()
            .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.AudioColumns.IS_MUSIC);
            for (cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()) {
                Song song = new Song();
                song.setSinger(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                song.setDuration(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
                song.setPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                song.setALLName(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
                song.setSong(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
                song.setSize(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
                song.setId(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
                if(song.getSong().contains("-")){
                    String Name[] = song.getSong().split("-");
                    String name = Name[1].toString().split(".mp3")[0].indexOf("[mqms2]") > 0
                            ? Name[1].toString().split(".mp3")[0].substring(0, Name[1].toString().split(".mp3")[0].indexOf("[mqms2]")) : Name[1].toString().split(".mp3")[0];
                    song.setSong(name);
                    song.setSinger(Name[0]);
                }
                if (song.getDuration()/(1000 * 60) >= 1) {     //只把1分钟以上的音乐添加到集合当中
                    song.setPosition(position);
                    list1.add(song);
                    Log.i("songsong","+"+song.getALLName()+song.getPath());
                    position++;
                }
                Log.i("getMUSIC","TTTTTTTTTTT3"+list1.size());
            }
             cursor.close();

       if (list == null) {
           list = list1;
       }
        return list1;
    }

RecyclerView的使用

添加布局

       <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:id="@+id/rv_fragment_local"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>

写item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_song"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="人海 - 2015 版"
         android:layout_marginTop="5dp"
         android:layout_marginLeft="8dp"
         android:textColor="#000000"
         android:textSize="16sp"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="8dp">
        <TextView
            android:id="@+id/tv_size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:text="6.0M" />
        <TextView
            android:id="@+id/tv_singer"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/tv_size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:text="燕池"
            />
    </RelativeLayout>
    <View
        android:background="#e2dada"
        android:layout_marginLeft="8dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="3dp"/>

</LinearLayout>

写适配器

写一个MusicAdapter,在里面写一个MusicViewHolder 继承 RecyclerView.ViewHolder 会提供一个方法~用来绑定控件

public class MusicAdapter {
    public class MusicViewHolder extends RecyclerView.ViewHolder {
        public MusicViewHolder(View itemView) {
            super(itemView);
        }
    }
}

然后让MusicAdapter继承RecyclerView.Adapter

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MusicViewHolder>{
    List<Song> list;

    private Context context;


    public MusicAdapter (List<Song> list,Context context) {
        this.list = list;
        this.context = context;
    }
    @Override
    public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MusicViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music,parent,false));
    }//绑定视图
    @Override
    public void onBindViewHolder(MusicViewHolder holder, int position) {
    holder.load(list.get(position),context);
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public class MusicViewHolder extends RecyclerView.ViewHolder{
         TextView tvSong;
         TextView tvSinger;
         TextView tvSize;
         public MusicViewHolder(View itemView) {
             super(itemView);
             tvSinger = itemView.findViewById(R.id.tv_singer);
             tvSong = itemView.findViewById(R.id.tv_song);
             tvSize = itemView.findViewById(R.id.tv_size);
         }
         public void load (final Song song, final Context context){
             tvSinger.setText(song.getSinger());
             tvSize.setText(convertFileSize(song.getSize()));
             tvSong.setText(song.getSong());
             if (song.getPath().equals(MusicUtil.getInstance().getPre())){
                 Log.i(TAG, "RED"+song.getPath()+MusicUtil.getInstance().getPre());
                 tvSize.setTextColor(Color.parseColor("#da3318"));
                 tvSinger.setTextColor(Color.parseColor("#da3318"));
                 tvSong.setTextColor(Color.parseColor("#da3318"));
             }else{
                 tvSize.setTextColor(Color.parseColor("#959595"));
                 tvSinger.setTextColor(Color.parseColor("#959595"));
                 tvSong.setTextColor(Color.parseColor("#000000"));
             }
             itemView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     Intent intent = new Intent(context, com.music.activity.MusicActivity.class);
                     Bundle bundle = new Bundle();
                     bundle.putSerializable("songInfo",song);
                     intent.putExtras(bundle);
                     context.startActivity(intent);
                 }
             });
         }

Activity中的代码

  musicAdapter = new MusicAdapter(MusicUtil.getInstance().getLocalMusci(getActivity()));//将本地音乐的数据集合添加到适配器里面去
        rvFragmentLocal.setLayoutManager(new LinearLayoutManager(getContext()));
        rvFragmentLocal.setItemAnimator(new DefaultItemAnimator());//默认动画
        rvFragmentLocal.setAdapter(musicAdapter);
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值