安卓搜索音乐



我在android中搜索read song,我看到了来自你的代码:

private static ArrayList<SongModel> LoadSongsFromCard() {
    ArrayList<SongModel> songs = new ArrayList<SongModel>();
    // Filter only mp3s, only those marked by the MediaStore to be music and longer than 1 minute
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
            + " AND " + MediaStore.Audio.Media.MIME_TYPE + "= 'audio/mpeg'"
            + " AND " + MediaStore.Audio.Media.DURATION + " > 60000";
    final String[] projection = new String[] {
        MediaStore.Audio.Media._ID,             //0
        MediaStore.Audio.Media.TITLE,           //1
        MediaStore.Audio.Media.ARTIST,          //2
        MediaStore.Audio.Media.DATA,            //3
        MediaStore.Audio.Media.DISPLAY_NAME
    };
    final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
            + " COLLATE LOCALIZED ASC";
    Cursor cursor = null;
    try {
        // the uri of the table that we want to query
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //getContentUriForPath("");
        // query the db
        cursor = _context.getContentResolver().query(uri,
                projection, selection, null, sortOrder);
        if (cursor != null) {
            cursor.moveToFirst();                       
            while (!cursor.isAfterLast()) { 
                //if (cursor.getString(3).contains("AwesomePlaylists")) {
                    SongModel GSC = new SongModel();
                    GSC.ID = cursor.getLong(0);
                    GSC.songTitle = cursor.getString(1);
                    GSC.songArtist = cursor.getString(2);
                    GSC.path = cursor.getString(3);
                    // This code assumes genre is stored in the first letter of the song file name
                    String genreCodeString = cursor.getString(4).substring(0, 1);
                    if (!genreCodeString.isEmpty()) {
                        try {
                            GSC.genre = Short.parseShort(genreCodeString);
                        } catch (NumberFormatException ex) {
                            Random r = new Random();
                            GSC.genre = (short) r.nextInt(4);
                        } finally {
                            songs.add(GSC);
                        }
                    }
                //}
                cursor.moveToNext();
            }
        }
    } catch (Exception ex) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return songs;
}

我真的在学习为Android编程,并根据我的需要修改你的代码,但我很难理解你何时分配光标

cursor = _context.getContentResolver () query ( uri , projection , selection , null, sortOrder ) . ;

_context的一部分不理解,请您解释一下。

如果您的类扩展了活动,则可以将this用于_context。如果没有,则需要传递活动中的上下文。提到的第一个案例是这样的:

// query the db
cursor = this.getContentResolver().query(uri, projection, selection, null, sortOrder);

__

Android中的Context是什么?

最新更新