diff --git a/android/src/main/java/dev/zoriya/omni/OmniPlayer.kt b/android/src/main/java/dev/zoriya/omni/OmniPlayer.kt index 72eebb8..a7ab696 100644 --- a/android/src/main/java/dev/zoriya/omni/OmniPlayer.kt +++ b/android/src/main/java/dev/zoriya/omni/OmniPlayer.kt @@ -298,6 +298,48 @@ class OmniPlayer( .build() } + private fun sourceOf(item: MediaItem): Source { + val extras = item.requestMetadata.extras + val meta = item.mediaMetadata + return Source( + src = VideoSrc( + uri = item.localConfiguration?.uri?.toString() ?: item.mediaId, + mimeType = item.localConfiguration?.mimeType, + headers = emptyMap(), + ), + startTime = null, + subtitles = item.localConfiguration?.subtitleConfigurations.orEmpty().map { + Subtitle( + id = it.id ?: it.uri.toString(), + link = it.uri.toString(), + mimeType = it.mimeType, + language = it.language, + label = it.label, + ) + }.toTypedArray(), + fonts = null, + metadata = Metadata( + title = meta.title?.toString() ?: "", + album = meta.albumTitle?.toString(), + artist = meta.artist?.toString(), + imageLink = meta.artworkUri?.toString(), + // we did not load it, we don't know what comes around it. + hasPrev = null, + hasNext = null, + ), + mixAudio = null, + castId = extras?.getString(CAST_ID_EXTRA), + castData = extras?.getString(CAST_DATA_EXTRA)?.let { raw -> + try { + val json = JSONObject(raw) + json.keys().asSequence().associateWith { json.optString(it) } + } catch (_: Throwable) { + null + } + }, + ) + } + fun setVideoView(surfaceView: android.view.SurfaceView) { runOnMainThread { localPlayer.setVideoSurfaceView(surfaceView) diff --git a/src/provider.web.tsx b/src/provider.web.tsx index f78a198..44ccbef 100644 --- a/src/provider.web.tsx +++ b/src/provider.web.tsx @@ -1,13 +1,7 @@ import { createPlayer } from "@videojs/react"; import { GoogleCast } from "@videojs/react/media/google-cast"; import { videoFeatures } from "@videojs/react/video"; -import { - createContext, - type ReactNode, - useContext, - useEffect, - useRef, -} from "react"; +import { createContext, type ReactNode, useContext, useEffect } from "react"; import { usePlayerState } from "./events"; import { WebOmniPlayer } from "./player.web"; import type { OmniPlayer, PlayerBackend } from "./types/player"; @@ -58,16 +52,10 @@ const PlayerInitializer = ({ }) => { const store = VideoPlayer.usePlayer(); const player = useLazyRef(() => new WebOmniPlayer(store)); - const seekedForSrc = useRef(undefined); useEffect(() => { player.source = source; - const uri = source?.src?.uri; - if (uri !== seekedForSrc.current) { - seekedForSrc.current = uri; - if (source?.startTime) store.seek(source.startTime); - } - }, [source, store]); + }, [source]); useEffect(() => { player.showNotification = showNotification; diff --git a/src/view.web.tsx b/src/view.web.tsx index bec9728..66763a1 100644 --- a/src/view.web.tsx +++ b/src/view.web.tsx @@ -134,6 +134,17 @@ export const OmniView = ({ [], ); + const player = usePlayer(); + const uri = source?.src.uri; + const startTime = source?.startTime; + const prevUri = useRef(undefined); + useEffect(() => { + if (!uri) return; + if (startTime) player.currentTime = startTime; + if (autoplay && prevUri.current && prevUri.current !== uri) player.play(); + prevUri.current = uri; + }, [player, uri, startTime, autoplay]); + // While casting, the receiver renders subtitles (the player forwards the // selection); skip rendering them locally on the idle