Gère les interactions avec Cast. Objet singleton récupéré à l'aide de getInstance
Gère les états : fournit l'état de l'icône Cast et l'objet de session.
L'interaction avec la diffusion n'est pas disponible tant que les options de diffusion ne sont pas fournies par setOptions.
requestSession() renvoie une promesse contenant chrome.cast.ErrorCode pouvant avoir une valeur nulle.
Ouvre l'interface utilisateur de sélection de diffusion pour permettre à l'utilisateur de démarrer ou d'arrêter une session.
Génère
Error Si les options de contexte n'ont pas encore été fournies.
Renvoie
non-null Promise containing nullable chrome.cast.ErrorCode Une promesse indiquant que la session est en cours de démarrage ou a échoué (refusée avec un code d'erreur).
setLaunchCredentialsData
setLaunchCredentialsData(credentialsData)
Définit les données associées aux identifiants de l'utilisateur. Sera ignoré s'il est appelé avant setOptions. L'appel de setOptions après setLaunchCredentialsData effacera la valeur définie dans setLaunchCredentialsData.
non-null Error Si l'ID d'application est manquant dans les options.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/25 (UTC).
[null,null,["Dernière mise à jour le 2025/07/25 (UTC)."],[[["\u003cp\u003e\u003ccode\u003ecast.framework.CastContext\u003c/code\u003e is a singleton object that manages cast interactions and states, including the cast icon and session object.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods for managing sessions, such as starting and ending them, retrieving the current session state, and adding or removing event listeners.\u003c/p\u003e\n"],["\u003cp\u003eCast interaction requires setting cast options using \u003ccode\u003esetOptions\u003c/code\u003e, and credentials can be optionally set using \u003ccode\u003esetLaunchCredentialsData\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve the \u003ccode\u003eCastContext\u003c/code\u003e instance using \u003ccode\u003egetInstance\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003erequestSession\u003c/code\u003e method initiates the cast selection UI, enabling users to select a device and start a casting session.\u003c/p\u003e\n"]]],["The `CastContext` class manages cast interactions via a singleton instance. Key actions include: fetching the instance via `getInstance`, setting options with `setOptions`, and setting user credentials using `setLaunchCredentialsData`. It provides session management, allowing users to start sessions with `requestSession` and end them with `endCurrentSession`. It allows to manage event listeners via `addEventListener` and `removeEventListener` methods. `getCastState`, `getCurrentSession`, and `getSessionState` provide the current cast icon, session, and session states.\n"],null,["# Class: CastContext\n\ncast.[framework](/cast/docs/reference/web_sender/cast.framework).CastContext\n============================================================================\n\nclass static\n\nManages cast interaction. A singleton object that is fetched using\ngetInstance.\nManages states - provide the state of the cast icon, and session object.\nCast interaction is not supported till the cast options are provided by\nsetOptions.\n\nConstructor\n-----------\n\n### CastContext\n\nnew\nCastContext()\n\nMethods\n-------\n\n### getInstance\n\nstatic\n\ngetInstance() returns [cast.framework.CastContext](/cast/docs/reference/web_sender/cast.framework.CastContext)\n\nReturns the CastContext singleton instance.\n\nReturns\n\n: [cast.framework.CastContext](/cast/docs/reference/web_sender/cast.framework.CastContext) A singleton instance\n of the CastContext.\n\n### addEventListener\n\naddEventListener(type, handler)\n\nAdds an event listener.\n\n| #### Parameter ||\n|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| type | [cast.framework.CastContextEventType](/cast/docs/reference/web_sender/cast.framework#.CastContextEventType) Event type. Value must not be null. |\n| handler | (non-null function(non-null [cast.framework.CastStateEventData](/cast/docs/reference/web_sender/cast.framework.CastStateEventData)) or non-null function(non-null [cast.framework.SessionStateEventData](/cast/docs/reference/web_sender/cast.framework.SessionStateEventData))) |\n\n### endCurrentSession\n\nendCurrentSession(stopCasting)\n\nEnds current session.\n\n| #### Parameter ||\n|-------------|--------------------------------------------------------------------------|\n| stopCasting | boolean Should the receiver application be stopped or just disconnected. |\n\n### getCastState\n\ngetCastState() returns [cast.framework.CastState](/cast/docs/reference/web_sender/cast.framework#.CastState)\n\nReturns\n\n: `non-null `[cast.framework.CastState](/cast/docs/reference/web_sender/cast.framework#.CastState) Current cast icon state.\n\n### getCurrentSession\n\ngetCurrentSession() returns [cast.framework.CastSession](/cast/docs/reference/web_sender/cast.framework.CastSession)\n\nReturns\n\n: `nullable `[cast.framework.CastSession](/cast/docs/reference/web_sender/cast.framework.CastSession) Current session.\n\n### getSessionState\n\ngetSessionState() returns [cast.framework.SessionState](/cast/docs/reference/web_sender/cast.framework#.SessionState)\n\nReturns\n\n: `non-null `[cast.framework.SessionState](/cast/docs/reference/web_sender/cast.framework#.SessionState) Current session state.\n\n### removeEventListener\n\nremoveEventListener(type, handler)\n\nRemoves an event listener.\n\n| #### Parameter ||\n|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| type | [cast.framework.CastContextEventType](/cast/docs/reference/web_sender/cast.framework#.CastContextEventType) Event type. Value must not be null. |\n| handler | (non-null function(non-null [cast.framework.CastStateEventData](/cast/docs/reference/web_sender/cast.framework.CastStateEventData)) or non-null function(non-null [cast.framework.SessionStateEventData](/cast/docs/reference/web_sender/cast.framework.SessionStateEventData))) |\n\n### requestSession\n\nrequestSession() returns Promise containing nullable [chrome.cast.ErrorCode](/cast/docs/reference/web_sender/chrome.cast#.ErrorCode)\n\nOpens the cast selection UI, to allow user to start or stop session.\n\nThrows\n\n: `Error` If the context options were not provided yet.\n\nReturns\n\n: `non-null Promise containing nullable `[chrome.cast.ErrorCode](/cast/docs/reference/web_sender/chrome.cast#.ErrorCode) A promise to indicate session\n is starting, or failed (rejected with error code).\n\n### setLaunchCredentialsData\n\nsetLaunchCredentialsData(credentialsData)\n\nSets the user credentials data. Will be ignored if called before\nsetOptions. Calling setOptions after setLaunchCredentialsData will\nclear the value set in setLaunchCredentialsData.\n\n| #### Parameter ||\n|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| credentialsData | [chrome.cast.CredentialsData](/cast/docs/reference/web_sender/chrome.cast.CredentialsData) Credentials data used to identify the credentials of the sender. Value must not be null. |\n\n### setOptions\n\nsetOptions(options)\n\nSets the cast options.\nEvents will only be sent once config was provided.\n\n| #### Parameter ||\n|---------|---------------------------------------------------------------------------------------------------------------------------------------------|\n| options | [cast.framework.CastOptions](/cast/docs/reference/web_sender/cast.framework.CastOptions) Cast options for the page. Value must not be null. |\n\nThrows\n\n: `non-null Error` If the options are missing application id."]]