我是靠谱客的博主 听话雨,这篇文章主要介绍Android 获取本地音乐文件,现在分享给大家,希望可以做个参考。

  • 存储音乐文件的model
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class MusicInfo { private long id; private String title;//音乐名称 private String data;//音乐文件 private String album;//专辑 private String artist;//艺人 private int duration;//音乐时长 private long size;//音乐文件大小 public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } 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; } }
  • 通过ContentResolver查询数据存储到model
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
private static final Uri URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; private static final String[] MUSIC_PROJECTION = new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.SIZE }; private static final String SELECTION = "mime_type in ('audio/mpeg','audio/x-ms-wma') and bucket_display_name <> 'audio' and is_music > 0 "; private static final String SORT_ORDER = MediaStore.Audio.Media.DATA; private List<MusicInfo> getMusicInfo() { List<MusicInfo> musicInfos = new ArrayList<>(); ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(URI, MUSIC_PROJECTION, SELECTION, null, SORT_ORDER); if (cursor != null) { while (cursor.moveToNext()) { MusicInfo musicInfo = new MusicInfo(); long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)); musicInfo.setId(id); if (!TextUtils.isEmpty(title)) { musicInfo.setTitle(title); } if (!TextUtils.isEmpty(data)) { musicInfo.setData(data); } if (!TextUtils.isEmpty(album)) { musicInfo.setAlbum(album); } if (!TextUtils.isEmpty(artist)) { musicInfo.setArtist(artist); } if (duration != 0) { musicInfo.setDuration(duration); } if (size != 0) { musicInfo.setSize(size); } musicInfos.add(musicInfo); } cursor.close(); } return musicInfos; }

最后

以上就是听话雨最近收集整理的关于Android 获取本地音乐文件的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部