fix cast
This commit is contained in:
parent
e61b302f6e
commit
80c6065dfd
1 changed files with 29 additions and 7 deletions
|
|
@ -132,22 +132,44 @@ export function onCastSessionEnded(listener: () => void): () => void {
|
||||||
* 'play'/'pause'/'seeked' events, to know when to re-broadcast as host.
|
* 'play'/'pause'/'seeked' events, to know when to re-broadcast as host.
|
||||||
*/
|
*/
|
||||||
export async function startCast(contentUrl: string, title: string, onRemoteStateChanged: () => void): Promise<PlaybackDriver> {
|
export async function startCast(contentUrl: string, title: string, onRemoteStateChanged: () => void): Promise<PlaybackDriver> {
|
||||||
|
console.info(`Trying to cast ${title} from ${contentUrl}`);
|
||||||
const framework = window.cast!.framework;
|
const framework = window.cast!.framework;
|
||||||
|
const chromeCast = (window.chrome as any)?.cast;
|
||||||
|
|
||||||
const context = framework.CastContext.getInstance();
|
const context = framework.CastContext.getInstance();
|
||||||
await context.requestSession();
|
await context.requestSession();
|
||||||
|
|
||||||
const session = context.getCurrentSession();
|
const session = context.getCurrentSession();
|
||||||
if (!session) throw new Error('Aucune session Cast active après requestSession().');
|
if (!session) {
|
||||||
|
throw new Error('Aucune session Cast active après requestSession().');
|
||||||
|
}
|
||||||
|
|
||||||
const mediaInfo = new window.chrome!.cast!.media.MediaInfo(contentUrl, 'application/vnd.apple.mpegurl');
|
// 1. Initialisation de MediaInfo avec le MIME HLS universel
|
||||||
|
const mediaInfo = new chromeCast.media.MediaInfo(contentUrl, 'application/x-mpegurl');
|
||||||
|
if (chromeCast.media.StreamType) {
|
||||||
|
mediaInfo.streamType = chromeCast.media.StreamType.BUFFERED;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Structuration des metadonnées
|
||||||
|
if (chromeCast.media.GenericMediaMetadata) {
|
||||||
|
const metadata = new chromeCast.media.GenericMediaMetadata();
|
||||||
|
metadata.title = title;
|
||||||
|
mediaInfo.metadata = metadata;
|
||||||
|
} else {
|
||||||
mediaInfo.metadata = { title };
|
mediaInfo.metadata = { title };
|
||||||
await session.loadMedia(new window.chrome!.cast!.media.LoadRequest(mediaInfo));
|
}
|
||||||
|
|
||||||
|
// 3. Préparation et envoi de la requête de chargement
|
||||||
|
const request = new chromeCast.media.LoadRequest(mediaInfo);
|
||||||
|
request.autoplay = true;
|
||||||
|
|
||||||
|
await session.loadMedia(request as any);
|
||||||
|
|
||||||
const player = new framework.RemotePlayer();
|
const player = new framework.RemotePlayer();
|
||||||
const controller = new framework.RemotePlayerController(player);
|
const controller = new framework.RemotePlayerController(player);
|
||||||
|
|
||||||
controller.addEventListener(framework.RemotePlayerEventType.IS_PAUSED_CHANGED, onRemoteStateChanged);
|
controller.addEventListener(framework.RemotePlayerEventType.IS_PAUSED_CHANGED, onRemoteStateChanged);
|
||||||
controller.addEventListener(framework.RemotePlayerEventType.CURRENT_TIME_CHANGED, onRemoteStateChanged);
|
controller.addEventListener(framework.RemotePlayerEventType.CURRENT_TIME_CHANGED, onRemoteStateChanged);
|
||||||
|
console.info(`Casting ${title} from ${contentUrl}`);
|
||||||
return {
|
return {
|
||||||
get currentTime() { return player.currentTime; },
|
get currentTime() { return player.currentTime; },
|
||||||
set currentTime(seconds: number) { player.currentTime = seconds; controller.seek(); },
|
set currentTime(seconds: number) { player.currentTime = seconds; controller.seek(); },
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue