Retrieving Media from a Content Resolver

在设备中查找音乐:

ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
   
// query failed, handle error.
} else if (!cursor.moveToFirst()) {
   
// no media on the device
} else {
   
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
   
int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
   
do {
       
long thisId = cursor.getLong(idColumn);
       
String thisTitle = cursor.getString(titleColumn);
       
// ...process entry...
   
} while (cursor.moveToNext());
}

在MediaPlayer中使用它:

long id = /* retrieve it from somewhere */;
Uri contentUri = ContentUris.withAppendedId(
        android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);

mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(getApplicationContext(), contentUri);

// ...prepare and start...

 

你可能感兴趣的:(String,null,query,音乐)