获取SD卡中的音乐文件

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

小编最近在搞一个音乐播放器App,练练手; 首先遇到一个问题,怎么获取本地的音乐文件?


    /**
     * 获取SD卡中的音乐文件
     *
     * @param context
     * @return
     */
    public static ArrayList<Music> getMusicFile(Context context) {
        //ArrayList<Music>存放音乐
        ArrayList<Music> MusicFiles = new ArrayList<>();
        //查询媒体数据库
        ContentResolver resolver = context.getContentResolver();
         /** 
         * Uri:这个Uri代表要查询的数据库名称加上表的名称。
           这个Uri一般都直接从MediaStore里取得,例如我要取所有歌的信息,
          就必须利用MediaStore.Audio.Media. EXTERNAL _CONTENT_URI这个Uri。
         * 
         */
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        //遍历媒体数据库
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                //歌曲编号MediaStore.Audio.Media._ID
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));

                //歌曲标题
                String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));

                //歌曲的专辑名MediaStore.Audio.Media.ALBUM
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));

                //歌曲的歌手名MediaStore.Audio.Media.ARTIST
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));

                //歌曲文件的路径MediaStore.Audio.Media.DATA
                String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));

                //歌曲的总播放时长MediaStore.Audio.Media.DURATION
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));

                //歌曲文件的大小MediaStore.Audio.Media.SIZE
                long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

                if (size > 1024 * 800) {     //是否大于800K
                    if (title.equals("<unknown>") || title.equals("")) {
                        title = "未知";
                    }
                    if ("<unknown>".equals(artist) || "".equals(artist)) {
                        artist = "未知";
                    }

                    Music music = new Music(id, title, artist,
                            url, album, duration, size);
                    MusicFiles.add(music);
                }
                cursor.moveToNext();
            }
        }
        return MusicFiles;
    }

Music类:

public class Music  {
    private int id;
    private String name;
    private String singerName;
    private String url;
    private String album;
    private int totalTime;
    private long size;

    public Music(int id, String name, String singerName,
                 String url, String album, int totalTime, long size) {
        this.id = id;
        this.name = name;
        this.singerName = singerName;
        this.url = url;
        this.album = album;
        this.totalTime = totalTime;
        this.size = size;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSingerName() {
        return singerName;
    }

    public void setSingerName(String singerName) {
        this.singerName = singerName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }
}

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