音乐本地扫描工具类,返回一个集合

一个简单的扫描本地音乐工具类 返回一个集合

package com.panghaha.it.mymusicplayerdemo;

/***
 * ━━━━ Code is far away from ━━━━━━
 *     ()      ()
 *     ( )    ( )
 *     ( )    ( )
 *   ┏┛┻━━━┛┻┓
 *   ┃   ━   ┃
 *   ┃ ┳┛ ┗┳ ┃
 *   ┃   ┻   ┃
 *   ┗━┓   ┏━┛
 *     ┃   ┃
 *     ┃   ┗━━━┓
 *     ┃       ┣┓
 *     ┃       ┏┛
 *     ┗┓┓┏━┳┓┏┛
 *      ┃┫┫ ┃┫┫
 *      ┗┻┛ ┗┻┛
 * ━━━━ bug with the more protecting ━━━
 * 

* Created by PangHaHa12138 on 2017/7/4. */ import android.content.Context; import android.database.Cursor; import android.provider.MediaStore; import java.util.ArrayList; import java.util.List; /** * 音乐工具类,扫描系统里面的音频文件,返回一个list集合 */ public class MusicUtils { public static List getMusicData(Context context) { List list = new ArrayList(); // 媒体库查询语句(写一个工具类MusicUtils) Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC); if (cursor != null) { while (cursor.moveToNext()) { Song song = new Song(); song.song = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); song.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); if (song.size > 1000 * 800) { // 注释部分是切割标题,分离出歌曲名和歌手 (本地媒体库读取的歌曲信息不规范) if (song.song.contains("-")) { String[] str = song.song.split("-"); song.singer = str[0]; song.song = str[1]; } list.add(song); } } // 释放资源 cursor.close(); } return list; } /** * 定义一个方法用来格式化获取到的时间 */ public static String formatTime(int time) { if (time / 1000 % 60 < 10) { return time / 1000 / 60 + ":0" + time / 1000 % 60; } else { return time / 1000 / 60 + ":" + time / 1000 % 60; } } }

Song类:

package com.panghaha.it.mymusicplayerdemo;

/***
 * ━━━━ Code is far away from ━━━━━━
 *     ()      ()
 *     ( )    ( )
 *     ( )    ( )
 *   ┏┛┻━━━┛┻┓
 *   ┃   ━   ┃
 *   ┃ ┳┛ ┗┳ ┃
 *   ┃   ┻   ┃
 *   ┗━┓   ┏━┛
 *     ┃   ┃
 *     ┃   ┗━━━┓
 *     ┃       ┣┓
 *     ┃       ┏┛
 *     ┗┓┓┏━┳┓┏┛
 *      ┃┫┫ ┃┫┫
 *      ┗┻┛ ┗┻┛
 * ━━━━ bug with the more protecting ━━━
 * 

* Created by PangHaHa12138 on 2017/7/4. */ public class Song { public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public String getSong() { return song; } public void setSong(String song) { this.song = song; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } /** 歌手 */ public String singer; /** 歌曲名 */ public String song; /**歌曲的地址 */ public String path; /**歌曲长度 */ public int duration; /**歌曲的大小 */ public long size; }

你可能感兴趣的:(音乐本地扫描工具类,返回一个集合)