Cómo recuperar contenido multimedia de un agente de resolución de contenido
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Para que tu app de reproductor multimedia recupere la música que el usuario tiene en su dispositivo, consulta ContentResolver
para obtener contenido multimedia externo:
Kotlin
val resolver: ContentResolver = contentResolver
val uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val cursor: Cursor? = resolver.query(uri, null, null, null, null)
when {
cursor == null -> {
// query failed, handle error.
}
!cursor.moveToFirst() -> {
// no media on the device
}
else -> {
val titleColumn: Int = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE)
val idColumn: Int = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID)
do {
val thisId = cursor.getLong(idColumn)
val thisTitle = cursor.getString(titleColumn)
// ...process entry...
} while (cursor.moveToNext())
}
}
cursor?.close()
Java
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());
}
Para usar esta función con MediaPlayer
, puedes hacer lo siguiente:
Kotlin
val id: Long = /* retrieve it from somewhere */
val contentUri: Uri =
ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id )
mediaPlayer = MediaPlayer().apply {
setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
)
setDataSource(applicationContext, contentUri)
}
// ...prepare and start...
Java
long id = /* retrieve it from somewhere */;
Uri contentUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
);
mediaPlayer.setDataSource(getApplicationContext(), contentUri);
// ...prepare and start...
Más información
Jetpack Media3 es la solución recomendada para la reproducción de contenido multimedia en tu app. Obtén más información al respecto.
En estas páginas, se analizan temas relacionados con la grabación, el almacenamiento y la reproducción de audio y video:
El contenido y las muestras de código que aparecen en esta página están sujetas a las licencias que se describen en la Licencia de Contenido. Java y OpenJDK son marcas registradas de Oracle o sus afiliados.
Última actualización: 2025-07-27 (UTC)
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Falta la información que necesito","missingTheInformationINeed","thumb-down"],["Muy complicado o demasiados pasos","tooComplicatedTooManySteps","thumb-down"],["Desactualizado","outOfDate","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Problema con las muestras o los códigos","samplesCodeIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-07-27 (UTC)"],[],[],null,["# Retrieve media from a content resolver\n\nYou can have your media player application retrieve music that the user has\non their device by querying\nthe [`ContentResolver`](/reference/android/content/ContentResolver) for external media: \n\n### Kotlin\n\n val resolver: ContentResolver = contentResolver\n val uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI\n val cursor: Cursor? = resolver.query(uri, null, null, null, null)\n when {\n cursor == null -\u003e {\n // query failed, handle error.\n }\n !cursor.moveToFirst() -\u003e {\n // no media on the device\n }\n else -\u003e {\n val titleColumn: Int = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE)\n val idColumn: Int = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID)\n do {\n val thisId = cursor.getLong(idColumn)\n val thisTitle = cursor.getString(titleColumn)\n // ...process entry...\n } while (cursor.moveToNext())\n }\n }\n cursor?.close()\n\n### Java\n\n ContentResolver contentResolver = getContentResolver();\n Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;\n Cursor cursor = contentResolver.query(uri, null, null, null, null);\n if (cursor == null) {\n // query failed, handle error.\n } else if (!cursor.moveToFirst()) {\n // no media on the device\n } else {\n int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);\n int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);\n do {\n long thisId = cursor.getLong(idColumn);\n String thisTitle = cursor.getString(titleColumn);\n // ...process entry...\n } while (cursor.moveToNext());\n }\n\nTo use this with the [`MediaPlayer`](/reference/android/media/MediaPlayer), you can do this: \n\n### Kotlin\n\n val id: Long = /* retrieve it from somewhere */\n val contentUri: Uri =\n ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id )\n\n mediaPlayer = MediaPlayer().apply {\n setAudioAttributes(\n AudioAttributes.Builder()\n .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)\n .setUsage(AudioAttributes.USAGE_MEDIA)\n .build()\n )\n setDataSource(applicationContext, contentUri)\n }\n\n // ...prepare and start...\n\n### Java\n\n long id = /* retrieve it from somewhere */;\n Uri contentUri = ContentUris.withAppendedId(\n android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);\n\n mediaPlayer = new MediaPlayer();\n mediaPlayer.setAudioAttributes(\n new AudioAttributes.Builder()\n .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)\n .setUsage(AudioAttributes.USAGE_MEDIA)\n .build()\n );\n mediaPlayer.setDataSource(getApplicationContext(), contentUri);\n\n // ...prepare and start...\n\nLearn more\n----------\n\nJetpack Media3 is the recommended solution for media playback in your app. [Read\nmore](/media/media3) about it.\n\nThese pages cover topics relating to recording, storing, and playing back audio\nand video:\n\n- [Supported Media Formats](/guide/topics/media/media-formats)\n- [MediaRecorder](/guide/topics/media/mediarecorder)\n- [Data Storage](/guide/topics/data/data-storage)"]]