Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample MediaItemConverter for google cast
private const val KEY_EPISODE = "episode"
class MediaQueueItemMapper @Inject constructor(
private val gson: Gson,
private val mediaItemMapper: MediaItemMapper // Our custom implementation
) : MediaItemConverter {
override fun toMediaQueueItem(mediaItem: MediaItem): MediaQueueItem {
val episode = mediaItem.playbackProperties?.tag as Episode
return toMediaQueueItem(episode)
}
override fun toMediaItem(mediaQueueItem: MediaQueueItem): MediaItem {
return mediaItemMapper.toMediaItem(gson.fromJson(mediaQueueItem.customData.getString(KEY_EPISODE), Episode::class.java))
}
private fun toMediaQueueItem(episode: Episode): MediaQueueItem = with(episode) {
val mediaMetaData = MediaMetadata(MediaMetadata.MEDIA_TYPE_AUDIOBOOK_CHAPTER)
mediaMetaData.putString(MediaMetadata.KEY_SUBTITLE, author)
mediaMetaData.putString(MediaMetadata.KEY_TITLE, title)
val coverImage = if (coverImageWithFallback != null) {
coverImageWithFallback
} else {
BuildConfig.CAST_FALLBACK_COVER_URL
}
mediaMetaData.addImage(WebImage(Uri.parse(coverImage)))
val rawStreamUrl = Uri.parse(this.streamUrl).buildUrl()
val mediaInfo = MediaInfo.Builder(rawStreamUrl).apply {
setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
setMetadata(mediaMetaData)
}.build()
val mediaQueueItem = MediaQueueItem.Builder(mediaInfo).build()
mediaQueueItem.customData = JSONObject()
mediaQueueItem.customData.put(KEY_EPISODE, gson.toJson(episode))
return mediaQueueItem
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment