如何创建 CAF 接收器?
How to create CAF Receiver?
近期,Google将投射接收器升级至V3版本
在google演示页面中有一个简单的演示如下:
<html>
<head>
</head>
<body>
<cast-media-player id="player"></cast-media-player>
<style>
#player {
--theme-hue: 210;
--splash-image: url("my.png");
}
</style>
<script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js">
</script>
<script>
const context = cast.framework.CastReceiverContext.getInstance();
const playerManager = context.getPlayerManager();
// intercept the LOAD request to be able to read in a contentId and get data
playerManager.setMessageInterceptor(
cast.framework.messages.MessageType.LOAD, loadRequestData => {
if (loadRequestData.media && loadRequestData.media.contentId) {
return thirdparty.getMediaById(loadRequestData.media.contentId)
.then(media => {
if (media) {
loadRequestData.media.contentUrl = media.url;
loadRequestData.media.contentType = media.contentType;
loadRequestData.media.metadata = media.metadata;
}
return loadRequestData;
});
}
return loadRequestData;
});
// listen to all Core Events
playerManager.addEventListener(cast.framework.events.category.CORE,
event => {
console.log(event);
});
const MyCastQueue = class extends cast.framework.QueueBase {
initialize(loadRequestData) {
const media = loadRequestData.media;
const items = [];
items.push(myCreateItem(media)); // your custom function logic
const queueData = new cast.framework.messages.QueueData();
queueData.items = items;
return queueData;
}
nextItems(itemId) {
return [myCreateNextItem()]; // your custom function logic
}
};
const playbackConfig = new cast.framework.PlaybackConfig();
// Sets the player to start playback as soon as there are five seconds of
// media contents buffered. Default is 10.
playbackConfig.autoResumeDuration = 5;
const myCastQueue = new MyCastQueue(); // create instance of queue Object
context.start({queue: myCastQueue, playbackConfig: playbackConfig});
</script>
</body>
</html>
来自:
https://developers.google.com/cast/docs/caf_receiver_features#styling-the-player
但是我调试的时候出现了'thirdparty' is undefined
.
的错误
谁能告诉我如何创建 CAF 接收器?
这是示例代码。您应该自己提供这些额外功能。
该示例显示了如何覆盖排队系统以执行基于接收者的排队。恕我直言,这不是一个很好的例子,因为它没有提供异步获取基于服务器的队列的例子。
如果您不需要接收器管理的队列,您可以从上一页显示的最小 CAF 接收器开始。这足以让远程调试器正常工作,这至少是拥有自定义接收器的一个很好的理由。
https://developers.google.com/cast/docs/caf_receiver_basic
我做了一个更完整的例子。 https://gist.github.com/jcable/45c65a72074763a9ec30ddb1ff217517
近期,Google将投射接收器升级至V3版本
在google演示页面中有一个简单的演示如下:
<html>
<head>
</head>
<body>
<cast-media-player id="player"></cast-media-player>
<style>
#player {
--theme-hue: 210;
--splash-image: url("my.png");
}
</style>
<script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js">
</script>
<script>
const context = cast.framework.CastReceiverContext.getInstance();
const playerManager = context.getPlayerManager();
// intercept the LOAD request to be able to read in a contentId and get data
playerManager.setMessageInterceptor(
cast.framework.messages.MessageType.LOAD, loadRequestData => {
if (loadRequestData.media && loadRequestData.media.contentId) {
return thirdparty.getMediaById(loadRequestData.media.contentId)
.then(media => {
if (media) {
loadRequestData.media.contentUrl = media.url;
loadRequestData.media.contentType = media.contentType;
loadRequestData.media.metadata = media.metadata;
}
return loadRequestData;
});
}
return loadRequestData;
});
// listen to all Core Events
playerManager.addEventListener(cast.framework.events.category.CORE,
event => {
console.log(event);
});
const MyCastQueue = class extends cast.framework.QueueBase {
initialize(loadRequestData) {
const media = loadRequestData.media;
const items = [];
items.push(myCreateItem(media)); // your custom function logic
const queueData = new cast.framework.messages.QueueData();
queueData.items = items;
return queueData;
}
nextItems(itemId) {
return [myCreateNextItem()]; // your custom function logic
}
};
const playbackConfig = new cast.framework.PlaybackConfig();
// Sets the player to start playback as soon as there are five seconds of
// media contents buffered. Default is 10.
playbackConfig.autoResumeDuration = 5;
const myCastQueue = new MyCastQueue(); // create instance of queue Object
context.start({queue: myCastQueue, playbackConfig: playbackConfig});
</script>
</body>
</html>
来自: https://developers.google.com/cast/docs/caf_receiver_features#styling-the-player
但是我调试的时候出现了'thirdparty' is undefined
.
谁能告诉我如何创建 CAF 接收器?
这是示例代码。您应该自己提供这些额外功能。
该示例显示了如何覆盖排队系统以执行基于接收者的排队。恕我直言,这不是一个很好的例子,因为它没有提供异步获取基于服务器的队列的例子。
如果您不需要接收器管理的队列,您可以从上一页显示的最小 CAF 接收器开始。这足以让远程调试器正常工作,这至少是拥有自定义接收器的一个很好的理由。
https://developers.google.com/cast/docs/caf_receiver_basic
我做了一个更完整的例子。 https://gist.github.com/jcable/45c65a72074763a9ec30ddb1ff217517