미디어 세션 구현
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
만들기
MediaSession
드림
앱에서 미디어 재생을 준비할 때 표시됩니다. 다음 코드 스니펫
다음은 적절한 콜백과 플래그를 설정하는 방법의 예입니다.
Kotlin
session = MediaSession(this, "MusicService").apply {
setCallback(MediaSessionCallback())
setFlags(
MediaSession.FLAG_HANDLES_MEDIA_BUTTONS or MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
)
}
자바
session = new MediaSession(this, "MusicService");
session.setCallback(new MediaSessionCallback());
session.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
미디어 세션 시작
또한
setActive(true)
드림
재생 시작 시 앱은 다음에 설명된 대로 오디오 포커스도 요청해야 합니다.
오디오 포커스 관리: 이러한 단계는
다음 예에 나와 있습니다.
Kotlin
private fun handlePlayRequest() {
tryToGetAudioFocus()
if (!session.isActive) {
session.isActive = true
}
...
}
자바
private void handlePlayRequest() {
tryToGetAudioFocus();
if (!session.isActive()) {
session.setActive(true);
}
...
}
재생 상태 업데이트
다음에서 재생 상태를
반영하려면 MediaSession
현재 미디어의 상태:
Kotlin
private fun updatePlaybackState() {
val position: Long =
mediaPlayer
?.takeIf { it.isPlaying }
?.currentPosition?.toLong()
?: PlaybackState.PLAYBACK_POSITION_UNKNOWN
val stateBuilder = PlaybackState.Builder()
.setActions(getAvailableActions()).apply {
setState(mState, position, 1.0f)
}
session.setPlaybackState(stateBuilder.build())
}
private fun getAvailableActions(): Long {
var actions = (PlaybackState.ACTION_PLAY_PAUSE
or PlaybackState.ACTION_PLAY_FROM_MEDIA_ID
or PlaybackState.ACTION_PLAY_FROM_SEARCH)
playingQueue?.takeIf { it.isNotEmpty() }?.apply {
actions = if (mState == PlaybackState.STATE_PLAYING) {
actions or PlaybackState.ACTION_PAUSE
} else {
actions or PlaybackState.ACTION_PLAY
}
if (currentIndexOnQueue > 0) {
actions = actions or PlaybackState.ACTION_SKIP_TO_PREVIOUS
}
if (currentIndexOnQueue < size - 1) {
actions = actions or PlaybackState.ACTION_SKIP_TO_NEXT
}
}
return actions
}
자바
private void updatePlaybackState() {
long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN;
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
position = mediaPlayer.getCurrentPosition();
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(getAvailableActions());
stateBuilder.setState(mState, position, 1.0f);
session.setPlaybackState(stateBuilder.build());
}
private long getAvailableActions() {
long actions = PlaybackState.ACTION_PLAY_PAUSE |
PlaybackState.ACTION_PLAY_FROM_MEDIA_ID |
PlaybackState.ACTION_PLAY_FROM_SEARCH;
if (playingQueue == null || playingQueue.isEmpty()) {
return actions;
}
if (mState == PlaybackState.STATE_PLAYING) {
actions |= PlaybackState.ACTION_PAUSE;
} else {
actions |= PlaybackState.ACTION_PLAY;
}
if (currentIndexOnQueue > 0) {
actions |= PlaybackState.ACTION_SKIP_TO_PREVIOUS;
}
if (currentIndexOnQueue < playingQueue.size() - 1) {
actions |= PlaybackState.ACTION_SKIP_TO_NEXT;
}
return actions;
}
MediaMetadata
를 다음과 같이 설정합니다.
setMetadata()
메서드를 사용하여 축소하도록 요청합니다. 이 메서드는 재생되는 객체당 한 번만 호출됩니다. 이 도구를 사용하면
제목, 부제목,
예를 들면 다음과 같습니다. 다음은 음악 및
다음과 같이 트랙의 데이터가 맞춤 데이터 클래스 MediaData
에 저장되어 있다고 가정합니다.
Kotlin
private fun updateMetadata(myData: MediaData) {
val metadataBuilder = MediaMetadata.Builder().apply {
// To provide most control over how an item is displayed set the
// display fields in the metadata
putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, myData.displayTitle)
putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, myData.displaySubtitle)
putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, myData.artUri)
// And at minimum the title and artist for legacy support
putString(MediaMetadata.METADATA_KEY_TITLE, myData.title)
putString(MediaMetadata.METADATA_KEY_ARTIST, myData.artist)
// A small bitmap for the artwork is also recommended
putBitmap(MediaMetadata.METADATA_KEY_ART, myData.artBitmap)
// Add any other fields you have for your data as well
}
session.setMetadata(metadataBuilder.build())
}
Java
private void updateMetadata(MediaData myData) {
MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder();
// To provide most control over how an item is displayed set the
// display fields in the metadata
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE,
myData.displayTitle);
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE,
myData.displaySubtitle);
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI,
myData.artUri);
// And at minimum the title and artist for legacy support
metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE,
myData.title);
metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST,
myData.artist);
// A small bitmap for the artwork is also recommended
metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART,
myData.artBitmap);
// Add any other fields you have for your data as well
session.setMetadata(metadataBuilder.build());
}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Implement a media session\n\nCreate a\n[`MediaSession`](/reference/android/media/session/MediaSession#MediaSession(android.content.Context,%20java.lang.String))\nwhen your app is preparing to play media. The following code snippet\nis an example of how to set the appropriate callback and flags: \n\n### Kotlin\n\n```kotlin\nsession = MediaSession(this, \"MusicService\").apply {\n setCallback(MediaSessionCallback())\n setFlags(\n MediaSession.FLAG_HANDLES_MEDIA_BUTTONS or MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS\n )\n}\n```\n\n### Java\n\n```java\nsession = new MediaSession(this, \"MusicService\");\nsession.setCallback(new MediaSessionCallback());\nsession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |\n MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);\n```\n\nStart a media session\n---------------------\n\nBe sure to also call\n[`setActive(true)`](/reference/android/media/session/MediaSession#setActive(boolean))\nwhen playback begins. Your app must also request audio focus, as described in\n[Manage audio focus](/media/optimize/audio-focus). These steps are\nshown in the following example: \n\n### Kotlin\n\n```kotlin\nprivate fun handlePlayRequest() {\n\n tryToGetAudioFocus()\n\n if (!session.isActive) {\n session.isActive = true\n }\n ...\n}\n```\n\n### Java\n\n```java\nprivate void handlePlayRequest() {\n\n tryToGetAudioFocus();\n\n if (!session.isActive()) {\n session.setActive(true);\n }\n ...\n}\n```\n\nUpdate the playback state\n-------------------------\n\nUpdate the playback state in the\n[`MediaSession`](/reference/android/media/session/MediaSession) to reflect\nthe state of the current media: \n\n### Kotlin\n\n```kotlin\nprivate fun updatePlaybackState() {\n val position: Long =\n mediaPlayer\n ?.takeIf { it.isPlaying }\n ?.currentPosition?.toLong()\n ?: PlaybackState.PLAYBACK_POSITION_UNKNOWN\n\n val stateBuilder = PlaybackState.Builder()\n .setActions(getAvailableActions()).apply {\n setState(mState, position, 1.0f)\n }\n session.setPlaybackState(stateBuilder.build())\n}\n\nprivate fun getAvailableActions(): Long {\n var actions = (PlaybackState.ACTION_PLAY_PAUSE\n or PlaybackState.ACTION_PLAY_FROM_MEDIA_ID\n or PlaybackState.ACTION_PLAY_FROM_SEARCH)\n\n playingQueue?.takeIf { it.isNotEmpty() }?.apply {\n actions = if (mState == PlaybackState.STATE_PLAYING) {\n actions or PlaybackState.ACTION_PAUSE\n } else {\n actions or PlaybackState.ACTION_PLAY\n }\n if (currentIndexOnQueue \u003e 0) {\n actions = actions or PlaybackState.ACTION_SKIP_TO_PREVIOUS\n }\n if (currentIndexOnQueue \u003c size - 1) {\n actions = actions or PlaybackState.ACTION_SKIP_TO_NEXT\n }\n }\n return actions\n}\n```\n\n### Java\n\n```java\nprivate void updatePlaybackState() {\n long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN;\n if (mediaPlayer != null && mediaPlayer.isPlaying()) {\n position = mediaPlayer.getCurrentPosition();\n }\n PlaybackState.Builder stateBuilder = new PlaybackState.Builder()\n .setActions(getAvailableActions());\n stateBuilder.setState(mState, position, 1.0f);\n session.setPlaybackState(stateBuilder.build());\n}\n\nprivate long getAvailableActions() {\n long actions = PlaybackState.ACTION_PLAY_PAUSE |\n PlaybackState.ACTION_PLAY_FROM_MEDIA_ID |\n PlaybackState.ACTION_PLAY_FROM_SEARCH;\n if (playingQueue == null || playingQueue.isEmpty()) {\n return actions;\n }\n if (mState == PlaybackState.STATE_PLAYING) {\n actions |= PlaybackState.ACTION_PAUSE;\n } else {\n actions |= PlaybackState.ACTION_PLAY;\n }\n if (currentIndexOnQueue \u003e 0) {\n actions |= PlaybackState.ACTION_SKIP_TO_PREVIOUS;\n }\n if (currentIndexOnQueue \u003c playingQueue.size() - 1) {\n actions |= PlaybackState.ACTION_SKIP_TO_NEXT;\n }\n return actions;\n}\n```\n\nUpdate the media metadata\n-------------------------\n\nSet the [`MediaMetadata`](/reference/android/media/MediaMetadata) with the\n[`setMetadata()`](/reference/android/media/session/MediaSession#setMetadata(android.media.MediaMetadata))\nmethod. This method is only called once per object that is played. It lets you provide information to\nthe media session about the media, including its title, subtitle,\nartist, artwork, etc. The following example is tailored toward music and\nassumes the track's data is stored in a custom data class, `MediaData`: \n\n### Kotlin\n\n```kotlin\nprivate fun updateMetadata(myData: MediaData) {\n val metadataBuilder = MediaMetadata.Builder().apply {\n // To provide most control over how an item is displayed set the\n // display fields in the metadata\n putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, myData.displayTitle)\n putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, myData.displaySubtitle)\n putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, myData.artUri)\n // And at minimum the title and artist for legacy support\n putString(MediaMetadata.METADATA_KEY_TITLE, myData.title)\n putString(MediaMetadata.METADATA_KEY_ARTIST, myData.artist)\n // A small bitmap for the artwork is also recommended\n putBitmap(MediaMetadata.METADATA_KEY_ART, myData.artBitmap)\n // Add any other fields you have for your data as well\n }\n session.setMetadata(metadataBuilder.build())\n}\n```\n\n### Java\n\n```java\nprivate void updateMetadata(MediaData myData) {\n MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder();\n // To provide most control over how an item is displayed set the\n // display fields in the metadata\n metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE,\n myData.displayTitle);\n metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE,\n myData.displaySubtitle);\n metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI,\n myData.artUri);\n // And at minimum the title and artist for legacy support\n metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE,\n myData.title);\n metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST,\n myData.artist);\n // A small bitmap for the artwork is also recommended\n metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART,\n myData.artBitmap);\n // Add any other fields you have for your data as well\n session.setMetadata(metadataBuilder.build());\n}\n```"]]