Fix:Casting for podcast episodes #3044

This commit is contained in:
advplyr 2024-06-05 17:02:03 -05:00
parent 065aae9a7e
commit ef05e37a04
2 changed files with 41 additions and 25 deletions

View File

@ -29,4 +29,4 @@ export default class AudioTrack {
return this.contentUrl + `?token=${this.userToken}` return this.contentUrl + `?token=${this.userToken}`
} }
} }

View File

@ -1,13 +1,22 @@
function getMediaInfoFromTrack(libraryItem, castImage, track) { function getMediaInfoFromTrack(libraryItem, castImage, track) {
// https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.AudiobookChapterMediaMetadata let metadata = null
var metadata = new chrome.cast.media.AudiobookChapterMediaMetadata() if (libraryItem.mediaType === 'podcast') {
metadata.bookTitle = libraryItem.media.metadata.title metadata = new chrome.cast.media.MusicTrackMediaMetadata()
metadata.chapterNumber = track.index metadata.albumArtist = libraryItem.media.metadata.author
metadata.chapterTitle = track.title metadata.artist = libraryItem.media.metadata.author
metadata.images = [castImage] metadata.title = track.title
metadata.title = track.title metadata.albumName = libraryItem.media.metadata.title
metadata.subtitle = libraryItem.media.metadata.title metadata.images = [castImage]
} else {
// https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.AudiobookChapterMediaMetadata
metadata = new chrome.cast.media.AudiobookChapterMediaMetadata()
metadata.bookTitle = libraryItem.media.metadata.title
metadata.chapterNumber = track.index
metadata.chapterTitle = track.title
metadata.images = [castImage]
metadata.title = track.title
metadata.subtitle = libraryItem.media.metadata.title
}
var trackurl = track.fullContentUrl var trackurl = track.fullContentUrl
var mimeType = track.mimeType var mimeType = track.mimeType
@ -20,17 +29,25 @@ function getMediaInfoFromTrack(libraryItem, castImage, track) {
function buildCastMediaInfo(libraryItem, coverUrl, tracks) { function buildCastMediaInfo(libraryItem, coverUrl, tracks) {
const castImage = new chrome.cast.Image(coverUrl) const castImage = new chrome.cast.Image(coverUrl)
return tracks.map(t => getMediaInfoFromTrack(libraryItem, castImage, t)) return tracks.map((t) => getMediaInfoFromTrack(libraryItem, castImage, t))
} }
function buildCastQueueRequest(libraryItem, coverUrl, tracks, startTime) { function buildCastQueueRequest(libraryItem, coverUrl, tracks, startTime) {
var mediaInfoItems = buildCastMediaInfo(libraryItem, coverUrl, tracks) var mediaInfoItems = buildCastMediaInfo(libraryItem, coverUrl, tracks)
var containerMetadata = new chrome.cast.media.AudiobookContainerMetadata() let containerMetadata = null
containerMetadata.authors = libraryItem.media.metadata.authors.map(a => a.name) let queueType = chrome.cast.media.QueueType.AUDIOBOOK
containerMetadata.narrators = libraryItem.media.metadata.narrators || [] if (libraryItem.mediaType === 'podcast') {
containerMetadata.publisher = libraryItem.media.metadata.publisher || undefined queueType = chrome.cast.media.QueueType.PODCAST_SERIES
containerMetadata.title = libraryItem.media.metadata.title containerMetadata = new chrome.cast.media.ContainerMetadata(chrome.cast.media.ContainerType.GENERIC_CONTAINER)
containerMetadata.title = libraryItem.media.metadata.title
} else {
containerMetadata = new chrome.cast.media.AudiobookContainerMetadata()
containerMetadata.authors = libraryItem.media.metadata.authors?.map((a) => a.name)
containerMetadata.narrators = libraryItem.media.metadata.narrators || []
containerMetadata.publisher = libraryItem.media.metadata.publisher || undefined
containerMetadata.title = libraryItem.media.metadata.title
}
var mediaQueueItems = mediaInfoItems.map((mi) => { var mediaQueueItems = mediaInfoItems.map((mi) => {
var queueItem = new chrome.cast.media.QueueItem(mi) var queueItem = new chrome.cast.media.QueueItem(mi)
@ -38,23 +55,25 @@ function buildCastQueueRequest(libraryItem, coverUrl, tracks, startTime) {
}) })
// Find track to start playback and calculate track start offset // Find track to start playback and calculate track start offset
var track = tracks.find(at => at.startOffset <= startTime && at.startOffset + at.duration > startTime) var track = tracks.find((at) => at.startOffset <= startTime && at.startOffset + at.duration > startTime)
var trackStartIndex = track ? track.index - 1 : 0 var trackStartIndex = track ? track.index - 1 : 0
var trackStartTime = Math.floor(track ? startTime - track.startOffset : 0) var trackStartTime = Math.floor(track ? startTime - track.startOffset : 0)
var queueData = new chrome.cast.media.QueueData(libraryItem.id, libraryItem.media.metadata.title, '', false, mediaQueueItems, trackStartIndex, trackStartTime) var queueData = new chrome.cast.media.QueueData(libraryItem.id, libraryItem.media.metadata.title, '', false, mediaQueueItems, trackStartIndex, trackStartTime)
queueData.containerMetadata = containerMetadata queueData.containerMetadata = containerMetadata
queueData.queueType = chrome.cast.media.QueueType.AUDIOBOOK queueData.queueType = queueType
return queueData return queueData
} }
function castLoadMedia(castSession, request) { function castLoadMedia(castSession, request) {
return new Promise((resolve) => { return new Promise((resolve) => {
castSession.loadMedia(request) castSession.loadMedia(request).then(
.then(() => resolve(true), (reason) => { () => resolve(true),
(reason) => {
console.error('Load media failed', reason) console.error('Load media failed', reason)
resolve(false) resolve(false)
}) }
)
}) })
} }
@ -69,7 +88,4 @@ function buildCastLoadRequest(libraryItem, coverUrl, tracks, startTime, autoplay
return request return request
} }
export { export { buildCastLoadRequest, castLoadMedia }
buildCastLoadRequest,
castLoadMedia
}