feat: 添加对 Chromecast 状态和会话变化的监听支持

This commit is contained in:
Harvey Zack committed 2025-01-19 14:28:03 +08:00
1 parent e7c31e55b3
commit fae71811db
3 files changed
+139 -11

No files matched your search

@@ -32,6 +32,7 @@ export default function artplayerPluginChromecast(option) {
let isCastInitialized = false;
let castSession = null;
let castState = null;
const initializeCastApi = () => {
return new Promise((resolve, reject) => {
@@ -42,13 +43,57 @@ export default function artplayerPluginChromecast(option) {
receiverApplicationId: window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
autoJoinPolicy: window.chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED,
});
// Listen for session state changes
context.addEventListener(
window.cast.framework.CastContextEventType.SESSION_STATE_CHANGED,
(event) => {
const SessionState = window.cast.framework.SessionState;
castState = event.sessionState;
castSession = event.session;
updateCastButton();
switch (event.sessionState) {
case SessionState.NO_SESSION:
option.onStateChange?.('disconnected');
updateCastButton('disconnected');
break;
case SessionState.SESSION_STARTING:
option.onStateChange?.('connecting');
updateCastButton('connecting');
break;
case SessionState.SESSION_STARTED:
option.onStateChange?.('connected');
updateCastButton('connected');
break;
case SessionState.SESSION_ENDING:
option.onStateChange?.('disconnecting');
updateCastButton('disconnecting');
break;
case SessionState.SESSION_RESUMED:
option.onStateChange?.('connected');
updateCastButton('connected');
break;
}
},
);
// Listen for cast state changes
context.addEventListener(window.cast.framework.CastContextEventType.CAST_STATE_CHANGED, (event) => {
const CastState = window.cast.framework.CastState;
switch (event.castState) {
case CastState.NO_DEVICES_AVAILABLE:
option.onCastAvailable?.(false);
break;
case CastState.NOT_CONNECTED:
option.onCastAvailable?.(true);
break;
case CastState.CONNECTING:
case CastState.CONNECTED:
option.onCastAvailable?.(true);
break;
}
});
isCastInitialized = true;
resolve();
} else {
@@ -67,17 +112,33 @@ export default function artplayerPluginChromecast(option) {
const request = new window.chrome.cast.media.LoadRequest(mediaInfo);
session
.loadMedia(request)
.then(() => (art.notice.show = 'Casting started'))
.then(() => {
art.notice.show = 'Casting started';
option.onCastStart?.();
})
.catch((error) => {
art.notice.show = 'Error casting media';
option.onError?.(error);
throw error;
});
};
const updateCastButton = () => {
const updateCastButton = (state) => {
const button = document.querySelector('.art-icon-cast');
if (button) {
button.style.color = castSession ? 'red' : 'white';
switch (state) {
case 'connected':
button.style.color = 'red';
break;
case 'connecting':
case 'disconnecting':
button.style.color = 'orange';
break;
case 'disconnected':
default:
button.style.color = 'white';
break;
}
}
};
@@ -93,6 +154,7 @@ export default function artplayerPluginChromecast(option) {
await initializeCastApi();
} catch (error) {
art.notice.show = 'Failed to initialize Cast API';
option.onError?.(error);
throw error;
}
}
@@ -106,13 +168,18 @@ export default function artplayerPluginChromecast(option) {
castVideo(art, session);
} catch (error) {
art.notice.show = 'Error connecting to cast session';
option.onError?.(error);
throw error;
}
}
},
});
return { name: 'artplayerPluginChromecast' };
return {
name: 'artplayerPluginChromecast',
getCastState: () => castState,
isCasting: () => castSession !== null,
};
};
}