Skip to content

Commit

Permalink
Catch exceptions during desktop MediaSession interaction (closes #394)
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed Nov 11, 2024
1 parent 53781e8 commit 36debac
Showing 1 changed file with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,52 @@ internal fun createDesktopMediaSession(service: PlayerService): MediaSession? {
)
}
catch (e: Throwable) {
RuntimeException("Ignoring exception during MediaSession creation", e).printStackTrace()
RuntimeException("Ignoring exception while creating MediaSession", e).printStackTrace()
return null
}

if (session == null) {
return null
}

session.setIdentity("spmp")
fun withSession(block: MediaSession.() -> Unit) {
try {
block(session)
}
catch (e: Throwable) {
RuntimeException("Ignoring exception while accessing MediaSession", e).printStackTrace()
}
}

try {
session.setIdentity("spmp")
}
catch (e: Throwable) {
RuntimeException("Ignoring exception setting MediaSession identity", e).printStackTrace()
return null
}

val listener: PlayerListener =
object : PlayerListener() {
override fun onSongTransition(song: Song?, manual: Boolean) {
session.onPositionChanged()
session.onSongChanged(song, service)
override fun onSongTransition(song: Song?, manual: Boolean) = withSession {
onPositionChanged()
onSongChanged(song, service)
}
override fun onPlayingChanged(is_playing: Boolean) {
session.setPlaybackStatus(
override fun onPlayingChanged(is_playing: Boolean) = withSession {
setPlaybackStatus(
if (is_playing) MediaSessionPlaybackStatus.PLAYING
else MediaSessionPlaybackStatus.PAUSED
)
}
override fun onDurationChanged(duration_ms: Long) {
session.setMetadata(
override fun onDurationChanged(duration_ms: Long) = withSession {
setMetadata(
session.metadata.copy(
length_ms = duration_ms
)
)
}
override fun onSeeked(position_ms: Long) {
session.onPositionChanged()
override fun onSeeked(position_ms: Long) = withSession {
onPositionChanged()
}
}
service.addListener(listener)
Expand All @@ -58,33 +73,38 @@ internal fun createDesktopMediaSession(service: PlayerService): MediaSession? {
listener.onPlayingChanged(service.is_playing)
listener.onDurationChanged(service.duration_ms)

session.onPlayPause = {
service.playPause()
}
session.onPlay = {
service.play()
}
session.onPause = {
service.pause()
}
session.onNext = {
service.seekToNext()
}
session.onPrevious = {
service.seekToPrevious()
}
session.onSeek = { by_ms ->
service.seekTo(service.current_position_ms + by_ms)
try {
session.onPlayPause = {
service.playPause()
}
session.onPlay = {
service.play()
}
session.onPause = {
service.pause()
}
session.onNext = {
service.seekToNext()
}
session.onPrevious = {
service.seekToPrevious()
}
session.onSeek = { by_ms ->
service.seekTo(service.current_position_ms + by_ms)
}
session.onSetPosition = { to_ms ->
service.seekTo(to_ms)
}
}
session.onSetPosition = { to_ms ->
service.seekTo(to_ms)
catch (e: Throwable) {
RuntimeException("Ignoring exception while setting MediaSession callbacks", e).printStackTrace()
}

try {
session.setEnabled(true)
}
catch (e: Throwable) {
RuntimeException("Ignoring exception when enabling media session", e).printStackTrace()
RuntimeException("Ignoring exception while enabling MediaSession", e).printStackTrace()
return null
}

Expand Down

0 comments on commit 36debac

Please sign in to comment.