Web Receiver 应用的生命周期从 Web Receiver 的生命周期开始
然后一直进行到
并且 Cast 设备会还原为其默认状态。
在网络接收器应用的整个生命周期内,
Web Receiver 和所有已连接的发送器应用。发送者应用将
向 Google Cast 设备发送一条初始消息,请求创建会话
使用特定的应用 ID。这就开始了网络接收器的生命周期,因为
Google Cast 设备将尝试加载网络接收器应用。假设
没有网络问题,系统会从网络下载 Web Receiver 应用
使用与应用 ID 关联的解析网址。加载完成后,Web Receiver 会
应用将执行其设置操作,并指明其已可处理
来自任何已连接的发件人应用的邮件。
在开发 Web Receiver 应用之前,您需要
包含 Google Cast SDK Developer Console 的接收器应用。请参阅
注册以了解详情。所有网络接收器
应用要求发送者的应用为命令消息提供应用 ID
向网络接收器发送数据当您注册 Web 应用
接收方应用,您将收到要添加到发送者的
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"]],["最后更新时间 (UTC):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."]]