コンテンツ リゾルバからメディアを取得する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
メディア プレーヤー アプリで、ユーザーがデバイスに保存した音楽を取得するには、外部メディアの 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()
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());
}
これを 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...
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...
詳細
アプリでのメディア再生には、Jetpack Media3 が推奨されるソリューションです。詳しくは、こちらをご覧ください。
以下は音声と動画の録音、録画、保存、再生に関するトピックを扱うページです。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は 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)"]]