콘텐츠 리졸버에서 미디어 검색
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
미디어 플레이어 애플리케이션에서 외부 미디어의 ContentResolver
를 쿼리하여 사용자가 기기에 가지고 있는 음악을 검색할 수 있습니다.
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()
자바
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
로 이 기능을 사용하려면 다음을 실행하면 됩니다.
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...
자바
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...
자세히 알아보기
Jetpack Media3는 앱에서 미디어를 재생하는 데 권장되는 솔루션입니다. 자세한 내용은 자세히 알아보세요.
이 페이지에서는 오디오와 동영상 녹음/녹화, 저장 및 재생과 관련된 주제를 다룹니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 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)"]]