扫描歌曲
Posted Pres_Cheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扫描歌曲相关的知识,希望对你有一定的参考价值。
/** * 扫描歌曲 */ 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; }
以上是关于扫描歌曲的主要内容,如果未能解决你的问题,请参考以下文章