Récupérer des contenus multimédias à partir d'un outil de résolution de contenu
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Vous pouvez demander à votre application de lecteur multimédia de récupérer la musique que l'utilisateur a sur son appareil en interrogeant ContentResolver
pour les contenus multimédias externes:
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());
}
Pour utiliser cette méthode avec MediaPlayer
, procédez comme suit:
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...
En savoir plus
Jetpack Media3 est la solution recommandée pour la lecture multimédia dans votre application. En savoir plus
Ces pages traitent des sujets liés à l'enregistrement, au stockage et à la lecture d'audio et de vidéo:
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)"]]