Mengambil media dari ContentResolver
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Anda dapat meminta aplikasi pemutar media mengambil musik yang dimiliki pengguna
di perangkat mereka dengan membuat kueri
ContentResolver
untuk media eksternal:
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());
}
Untuk menggunakannya dengan MediaPlayer
, Anda dapat melakukan hal berikut:
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...
Pelajari lebih lanjut
Jetpack Media3 adalah solusi yang direkomendasikan untuk pemutaran media di aplikasi Anda. Baca
selengkapnya tentang hal ini.
Halaman ini membahas topik yang berkaitan dengan perekaman, penyimpanan, dan pemutaran kembali
audio dan video:
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)"]]