woong's

Android 내장 Music 불러오기 본문

Develop/Android

Android 내장 Music 불러오기

dlsdnd345 2016. 2. 14. 12:54

Android 내장 Music 불러오기


Anroid 폰 내의 내장 MP3 파일 불러오기 부분을 나중에도 쓸수 있도록 클래스화 했습니다 .



1. 노래를 담기위한 객체 


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
package com.handstudio.android.networkaudio.entities;
 
import android.net.Uri;
 
public class Music {
 
    Uri musicUri;
    String albumUri;
    String musicTitle;
    String singer;
    
    public Uri getMusicUri() {
        return musicUri;
    }
    public void setMusicUri(Uri musicUri) {
        this.musicUri = musicUri;
    }
    public String getAlbumUri() {
        return albumUri;
    }
    public void setAlbumUri(String albumUri) {
        this.albumUri = albumUri;
    }
    public String getMusicTitle() {
        return musicTitle;
    }
    public void setMusicTitle(String musicTitle) {
        this.musicTitle = musicTitle;
    }
    public String getSinger() {
        return singer;
    }
    public void setSinger(String singer) {
        this.singer = singer;
    }
    
    
}
 



2. 노래를 불러오는 객체


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.handstudio.android.networkaudio.utils;
 
import java.util.ArrayList;
 
import com.handstudio.android.networkaudio.entities.Music;
 
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
 
 
public class LoadMusic {
 
    private Context                 context;
    private ArrayList<Music>         musicList;
    
    public LoadMusic(Context context){
        
        this.context = context;
        musicList = new ArrayList<Music>();
        getMusicInfo();
    }
    
    public ArrayList<Music> getMusicList() {
        return musicList;
    }
 
    public void getMusicInfo(){
        String[] proj = {
                MediaStore.Audio.Media._ID, 
                MediaStore.Audio.Media.ALBUM_ID, 
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.MIME_TYPE
        };
 
        Cursor musicCursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, nullnullnull);
 
        if (musicCursor != null && musicCursor.moveToFirst()){
            String musicID;
            String albumID;
            String musicTitle;
            String singer;
            String mime;
 
            int musicIDCol = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int albumIDCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
            int musicTitleCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int singerCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int mimeCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE);
            do {
 
 
                musicID = musicCursor.getString(musicIDCol);
                albumID = musicCursor.getString(albumIDCol);
                musicTitle = musicCursor.getString(musicTitleCol);
                singer = musicCursor.getString(singerCol);
                mime = musicCursor.getString(mimeCol);
 
                if(mime.equals("audio/mpeg")){
                    
                    Music music = new Music();
                    String uri = getAlbumArt(Integer.parseInt(albumID)).toString();
                    music.setAlbumUri(uri);
                    
                    Uri musicURI = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, "" + musicID); 
                    
                    music.setMusicUri(musicURI);
                    music.setMusicTitle(musicTitle);
                    music.setSinger(singer);
                    musicList.add(music);
                }
                
            }while (musicCursor.moveToNext());
        }
        musicCursor.close();
        return;
    }
    
    public Uri getAlbumArt(long albumId)
    {
        Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
        Uri uri = ContentUris.withAppendedId(artworkUri, albumId);
        return uri;
    }
}
 


​3. 사용방법 


1
2
3
4
5
    private ArrayList<Music>     musicList;
 
        LoadMusic loadMusic = new LoadMusic(context);
        musicList = loadMusic.getMusicList();
 

 

 

 

​위와 같이 사용하면 내장 되어있는 MP3 파일들을 

List<Music> 을 통해서 사용할수 있습니다 .



'Develop > Android' 카테고리의 다른 글

Android service Message 전송하기  (0) 2016.02.14
Android Intent Flag 사용하기  (0) 2016.02.14
Android Service 사용 하기  (0) 2016.02.14
Android SurfaceView draw block 하기  (0) 2016.02.14
Android SurfaceView 배경 투명 처리  (0) 2016.02.14
Comments