Web Receiver 應用程式生命週期是從 Web 接收器起算的
都會載入投放裝置中,並跳到
應用程式停止後,投放裝置會還原成預設狀態。
在 Web 接收器應用程式的生命週期中,訊息會在
Web Receiver 和任何已連線的傳送者應用程式。傳送方應用程式
傳送初始訊息給 Google Cast 裝置,要求建立工作階段
指定應用程式的 ID這會啟動 Web 接收器的生命週期,
Google Cast 裝置將嘗試載入 Web Receiver 應用程式。假設
沒有網路問題,系統會從網路下載 Web Receiver 應用程式
使用與應用程式 ID 相關聯的解析網址載入後,網路接收端
應用程式會執行設定作業,並表示已準備好進行處理
傳送訊息。
Web Receiver 應用程式可能會撕裂 (結束目前的生命週期,並關閉
應用程式),並符合下列條件:
開發 Web Receiver 應用程式前,您必須先註冊
透過 Google Cast SDK 開發人員控制台使用接收端應用程式。詳情請見
Registration 瞭解詳情。所有網路接收器
應用程式要求傳送者應用程式提供含有指令訊息的應用程式 ID
然後透過寄件人 API 將要求傳送至網路接收器。註冊網站時
接收者應用程式會收到應用程式 ID,您可將此 ID 加入寄件者的
以及 API 呼叫
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eThe Web Receiver SDK simplifies media playback with a built-in player, automatically supporting Google Assistant and Cast-specific features across devices.\u003c/p\u003e\n"],["\u003cp\u003eTo build a basic Custom Web Receiver, register your app with the Google Cast SDK Developer Console and include the provided code snippet in your app.\u003c/p\u003e\n"],["\u003cp\u003eThe Cast framework's built-in media player, represented by the \u003ccode\u003ecast-media-player\u003c/code\u003e element, handles streaming protocols like MPEG-DASH, HLS, and Smooth Streaming with support for various media types.\u003c/p\u003e\n"],["\u003cp\u003eCross-Origin Resource Sharing (CORS) is fully supported for secure content access; ensure your content server is configured correctly for streaming content.\u003c/p\u003e\n"]]],["The document outlines creating a Custom Web Receiver app for Cast-enabled devices. Key actions include: registering the app in the Google Cast SDK Developer Console, referencing the Web Receiver SDK via `gstatic.com`, and creating a basic HTML5 app with a `cast-media-player` element. The SDK provides a built-in media player supporting streaming protocols and handles media playback. The app life cycle involves message exchanges between the receiver and sender applications. The document highlights utilizing Cross-Origin Resource Sharing (CORS) for streaming content.\n"],null,["# Custom Web Receiver\n\n**Jump start** - The code for creating a basic Custom Web Receiver is simple. Skip ahead to [register your Web Receiver\napp](#register_your_web_receiver_app), then [create a basic\nWeb Receiver app](#create_a_basic_web_receiver_app).\n\nThe Web Receiver SDK uses a built-in media player to provide a seamless and easy\nplayback experience. It provides out-of-box support for Google Assistant as well\nas Cast-specific features that are automatically supported on all senders and\ntouch-enabled devices. As new features are released they will continue to be\nsupported without additional sender changes.\n\nThe Custom Web Receiver is a custom-built HTML5 app that must be hosted to\ndisplay your content on Cast-enabled devices. You may need to create a Custom\nWeb Receiver depending on your business needs. For help deciding which receiver\ntype to use, see the\n[choose a Web Receiver guide](/cast/docs/web_receiver#choose_a_web_receiver).\n\nGoogle Web Receiver SDK\n-----------------------\n\nYour Web Receiver app accesses the Web Receiver API with the following\nreference: \n\n \u003cscript src=\"//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js\"\u003e\u003c/script\u003e\n\n**Best practice for URL protocols** : Notice the URL above does not specify an\n\"http:\" or \"https:\" protocol. Omitting these protocols when sourcing the\n`cast_receiver_framework.js` resource enables this resource to be fetched using\nthe same protocol as the server hosting the Web Receiver app. This means that\nswitching between HTTP for development and HTTPS for production is transparent\nand will not require a code change. (Published Web Receiver apps must be hosted\non TLS-capable servers.)\n| **Do not self-host the\n| `cast_receiver_framework.js` resource (Web Receiver SDK), not even\n| for local development.** Always use the Web Receiver SDK hosted by Google using the gstatic.com reference. The Web Receiver SDK hosted by Google is kept at parity with the platform firmware, and is updated periodically to address bug fixes and add new features. During development, you can host your Web Receiver app locally on a private network for testing, but hosting the Web Receiver SDK locally could result in your app breaking.\n\n### SDK preview\n\nA pre-release version of the Cast Web Receiver SDK is also available for use in\ntesting non-production applications. More information about the SDK preview URL\nmay be found at [Google Cast Web Receiver SDK Preview URL](/cast/preview-url).\n\nApplication life cycle\n----------------------\n\nThe Web Receiver app life cycle starts from the point at which the Web Receiver\nis loaded onto the Cast device and proceeds to the point at which the\napplication is torn down and the Cast device reverts back to its default state.\n\nOver the life cycle of a Web Receiver app, messages are exchanged between the\nWeb Receiver and any connected sender applications. A sender application will\nsend an initial message to a Google Cast device requesting a session be created\nusing a specific app ID. This kicks off the life cycle of the Web Receiver, as\nthe Google Cast device will attempt to load the Web Receiver app. Assuming there\nare no network issues, the Web Receiver app will be downloaded from the network\nusing the resolved URL associated with the app ID. Once loaded, the Web Receiver\napp will perform its setup operations and indicate that it is ready to process\nmessages from any connected sender applications.\n\nA Web Receiver app may tear down (end its current life cycle and close the\napplication) under the following conditions:\n\n- The Web Receiver app gets an explicit message from **the last** connected sender to end the application session.\n- The Web Receiver app has been idle for a defined period of time without any connected senders and decides to end the application session.\n- A different cast session was started.\n- The Web Receiver encounters a fatal error during its normal life cycle.\n\nThe Web Receiver SDK handles all common cases in accordance with our\n[UX guidelines](/cast/docs/ux_guidelines).\n\nMajor classes\n-------------\n\nThe Web Receiver SDK framework has 2 major classes:\n\n- [`cast.framework.CastReceiverContext`](/cast/docs/reference/caf_receiver/cast.framework.CastReceiverContext) - Manages\n overall framework and loads any necessary libraries. With this object, you can:\n\n - Set application configuration options\n - Handle system events (such as sender connected or disconnected)\n - Create custom channels\n - Initiate cast communication\n- [`cast.framework.PlayerManager`](/cast/docs/reference/caf_receiver/cast.framework.PlayerManager) - Manages media\n playback. It handles the underlying player and media element according to the\n request from the sender. With this object, you can:\n\n - Handle playback operations\n - Handle playback-related requests from sender\n - Handle playback-related events\n\nRegister your Web Receiver app\n------------------------------\n\nBefore developing a Web Receiver app, you will need to register your Web\nReceiver app with the Google Cast SDK Developer Console. See\n[Registration](/cast/docs/registration) for more information. All Web Receiver\napps require sender applications to provide an app ID with the command messages\nthey send to the Web Receiver through the sender API. When you register your Web\nReceiver application, you will receive the app ID to include in your sender's\nAPI calls.\n\nCreate a basic Web Receiver app\n-------------------------------\n\nThe following is the main structure of a basic Web Receiver app that has no\ncustomization:\n\n1. A `cast-media-player` HTML element to represent the media player.\n2. A script HTML element to load the Web Receiver framework.\n3. Call [`start()`](/cast/docs/reference/caf_receiver/cast.framework.CastReceiverContext#start) to start the Web Receiver app with no options.\n\nHere is the minimum code for a Web Receiver app using the Cast Application\nFramework without any customization. You can copy and paste this script exactly\nas-is into your app to create your Web Receiver app. \n\n \u003chtml\u003e\n \u003chead\u003e\n \u003cscript type=\"text/javascript\"\n src=\"//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js\"\u003e\n \u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003ccast-media-player\u003e\u003c/cast-media-player\u003e\n \u003cscript\u003e\n cast.framework.CastReceiverContext.getInstance().start();\n \u003c/script\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nAt this point, a user can open their sender app, connect to their Cast device,\nthen navigate to media and press Play, which tells the Web Receiver to stream\nthe media to the TV for the user to watch.\n\nCompare this basic Web Receiver with a\n[customized receiver app](/cast/docs/caf_receiver/advanced#creating-a-customized-receiver-app).\n\nMedia and players\n-----------------\n\nThe Cast framework provides a built-in media player, represented by the\n`cast-media-player` HTML element. This media player supports playback for\nstreaming protocols such as MPEG-DASH, HLS, and Smooth Streaming.\n\nA set of supported media codecs and containers are listed at\n[Supported Media](/cast/docs/media). Through Cast messaging, developers can\nsupport a list of sender-initiated operations such as load, play, pause and\nseek, where the Cast SDK handles the interactions with the media. For a list of\nsupported operations, refer to the sender API reference for your app's platform:\n[`RemoteMediaClient` in Android Sender](/android/reference/com/google/android/gms/cast/framework/media/RemoteMediaClient),\n[`GCKMediaControlChannel` in iOS Sender](/cast/docs/reference/ios/interface_g_c_k_remote_media_client)\nand\n[`Media` in Web Sender](/cast/docs/reference/chrome/chrome.cast.media.Media#methods).\n\n### Cross-Origin Resource Sharing\n\nGoogle Cast fully supports Cross-Origin Resource Sharing (CORS). Streaming\nprotocols, unlike most file-based protocols, access content in an asynchronous\nway using `XMLHttpRequest`. In a CORS world, these requests are guarded against\ninappropriate access by the CORS header from the server where the resource\noriginates. This means that your content's server has a say on where it may be\nincluded. Most modern browsers fully support CORS. iOS and Android devices\naccess the content at a lower level and do not look at these headers. This is\noften the first issue that comes up when a developer wishes to use streaming\ncontent. See [Cross-Origin Resource Sharing](/cast/docs/player#cors) for\ndetails."]]