音乐本地扫描工具类,返回一个集合
2017-07-05 17:14
176 查看
一个简单的扫描本地音乐工具类 返回一个集合
Song类:
package com.panghaha.it.mymusicplayerdemo; /*** * ━━━━ Code is far away from ━━━━━━ * () () * ( ) ( ) * ( ) ( ) * ┏┛┻━━━┛┻┓ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┻ ┃ * ┗━┓ ┏━┛ * ┃ ┃ * ┃ ┗━━━┓ * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ * ━━━━ bug with the more protecting ━━━ * <p/> * 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<Song> getMusicData(Context context) { List<Song> list = new ArrayList<Song>(); // 媒体库查询语句(写一个工具类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 ━━━ * <p/> * 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; }
相关文章推荐
- 扫描手机本地mp4视频文件并返回List集合
- 判断集合元素之间只要存在一个null值就返回但第一个元素不能为null
- 返回一个可用于循环访问集合的枚举数
- 返回一个集合对象,同时这个集合的对象的属性又是一个集合对象的处理方法(ViewModel)
- 记我的WiFi音箱之一个网络在线播放及本地播放音乐器(三)
- 记我的WiFi音箱之一个网络在线播放及本地播放音乐器(序)
- 如何返回一个只读泛型集合
- 给Array本地对象增加一个原型方法,他的用途是删除数组中重复的条目,并将新的数组返回
- 读编程之美:第一个编程题,写一个函数,返回数组所有数除于数组第一个数的结果集合
- 用list<类>集合接收一个网址返回的一个类的集合的XML
- 记我的WiFi音箱之一个网络在线播放及本地播放音乐器(五)
- 已知一个日期,yyyy-MM-dd格式,从当前日期倒推num天,返回这一日期集合
- 扫描IPHONE本地音乐文件,获得音乐名,歌手名代码示例
- mybatise查询返回的一个集合封装多个对象的实现
- java hibernate关联查询返回一个集合,转换成json报json死循环处理
- 在数据库查询语句中 要返回 一个集合的数据 的处理方式。
- 【C#Winform】一个多线程扫描本地磁盘目录文件个数的Demo
- 求大神指教 某 Action返回了一个集合 我想在多个页面使用该集合 配置文件该怎么写
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- 编写一个方法,返回某集合的所有子集