如何在没有应用歌曲的情况下列出MediaStore中的所有音乐

时间:2015-12-15 09:38:59

标签: android audio-player mpmusicplayercontroller audio android-music-player

我使用此代码获取MediaStore中的所有音乐,但不仅仅是音乐,我的列表中包含设备中的所有音乐,应用程序的声音类似于“facebook”或“skype”。如何避免?

public void getSongList(){
    //query external audio
    ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Toast.makeText(PlayListActivity.this, musicUri.getPath(), Toast.LENGTH_LONG).show();
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
    //iterate over results if valid
    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);
        int albumColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ALBUM_ID);
        //add songs to list
        Bitmap bmThumbnail;
        do {
            String path = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            long thisId = musicCursor.getLong(idColumn);
            long thisAlbumId = musicCursor.getLong(albumColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            songList.add(new Song(thisId, thisAlbumId, thisTitle, thisArtist, path));
        }
        while (musicCursor.moveToNext());
    }
}

1 个答案:

答案 0 :(得分:1)

我不确定这一点,但我是按照以下方式做到的。

public void getSongList(){
//query external audio
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Toast.makeText(PlayListActivity.this, musicUri.getPath(), Toast.LENGTH_LONG).show();
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
//iterate over results if valid
if(musicCursor!=null && musicCursor.moveToFirst()){
    //get columns
    int titleColumn = musicCursor.getColumnIndex
            (android.provider.MediaStore.Audio.Media.TITLE);
    int idColumn = musicCursor.getColumnIndex
            (android.provider.MediaStore.Audio.Media._ID);
    int artistColumn = musicCursor.getColumnIndex
            (android.provider.MediaStore.Audio.Media.ARTIST);
    int albumColumn = musicCursor.getColumnIndex
            (android.provider.MediaStore.Audio.Media.ALBUM_ID);
    int songDuration  = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DUration);
    //add songs to list
    Bitmap bmThumbnail;
    do {
        String path = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
        long thisId = musicCursor.getLong(idColumn);
        long thisAlbumId = musicCursor.getLong(albumColumn);
        String thisTitle = musicCursor.getString(titleColumn);
        String thisArtist = musicCursor.getString(artistColumn);
        int dur  =       Integer.parseInt(musicCursor.getString(songDuration));
        if(dur>5000){
        songList.add(new Song(thisId, thisAlbumId, thisTitle, thisArtist, path));}
        }
         while (musicCursor.moveToNext());
    }
}

另一种方法可能是使用目录吗?