码迷,mamicode.com
首页 > 其他好文 > 详细

扫描歌曲

时间:2017-06-18 15:13:47      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:void   blog   []   data   name   dia   list   filename   column   

/**
 * 扫描歌曲
 */
public static void scanMusic(Context context, List<Music> musicList) {
    musicList.clear();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
            MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    if (cursor == null) {
        return;
    }
    while (cursor.moveToNext()) {
        // 是否为音乐
        int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
        if (isMusic == 0) {
            continue;
        }
        long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
        // 标题
        String title = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
        // 艺术家
        String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
        // 专辑
        String album = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
        // 持续时间
        long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
        // 音乐uri
        String uri = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
        // 专辑封面id,根据该id可以获得专辑图片uri
        long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
        String coverUri = getCoverUri(context, albumId);
        // 音乐文件名
        String fileName = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
        // 音乐文件大小
        long fileSize = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
        // 发行时间
        String year = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.YEAR)));
        Music music = new Music();
        music.set...
        musicList.add(music);
    }
    cursor.close();
}

/**
 * 查询专辑封面图片uri
 */
private static String getCoverUri(Context context, long albumId) {
    String uri = null;
    Cursor cursor = context.getContentResolver().query(
            Uri.parse("content://media/external/audio/albums/" + albumId),
            new String[]{"album_art"}, null, null, null);
    if (cursor != null) {
        cursor.moveToNext();
        uri = cursor.getString(0);
        cursor.close();
    }
    CoverLoader.getInstance().loadThumbnail(uri);
    return uri;
}

 

扫描歌曲

标签:void   blog   []   data   name   dia   list   filename   column   

原文地址:http://www.cnblogs.com/prescheng/p/7044248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!