프로젝트에서 사용할 포어그라운드 서비스로 동작하는 플레이어 구현을 위한 모듈
MediaSessionService 를 상속받은 객체로,
MediaSessionService 은 대응되는 MediaSession이 활성화될때마다 자동으로 실행되어 Notification을 표시한다.
→ 다른 서비스처럼 startService()나 startForegroundService() 등을 호출하지 않아도 됨
클래스 내부에서 관리할 MediaSession 객체를 생성하고, onGetSession() 메소드에서 리턴하는 MediaSession을 이걸로 지정함으로서 이 MediaSessionService가 관리하는 MediaSession을 지정할 수 있음
여기선 onCreate()에서 서비스가 시작될때 이 MediaSession에 대응되는 Notification 생성을 위해 setMediaNotificationProvider() 호출
setMediaNotificationProvider()의 createNotification() 에서 리턴하는 것은 아래의 MediaNotificationProvider의 MediaNotification 제공함수.
결과적으로 이 MediaSession에 대한 커스텀 Notification을 설정하는 것임
MediaNotification을 생성하고 MediaSession과 연결, 이를 제공하는 클래스
val notificationBuilder = NotificationCompat.Builder(
context,
NOTIFICATION_CHANNEL_ID.toString()
).apply {
priority = NotificationCompat.PRIORITY_DEFAULT
setSilent(true)
setSmallIcon(R.drawable.ic_musicroad_foreground)
setContentTitle(mediaItem?.mediaMetadata?.title)
setContentIntent(createNotifyPendingIntent())
setDeleteIntent(
actionFactory.createMediaActionPendingIntent(
mediaSession,
Player.COMMAND_STOP.toLong()
)
)
setStyle(
MediaStyleNotificationHelper
.MediaStyle(mediaSession)
)
setOngoing(true)
PlayerCommands.entries.forEach { commandButton ->
addAction(
actionFactory.createCustomAction(
mediaSession,
IconCompat.createWithResource(
context,
commandButton.iconResId(mediaSession.player.isPlaying)
),
commandButton.displayName,
commandButton.customAction,
commandButton.sessionCommand.customExtras
)
)
}
}
NotificationCompat.Builder 를 통해 MediaNotification 을 생성하는데