使用 Google Cast - 媒体播放器库创建自定义接收器



我想为我的自定义接收器实现媒体播放器功能。在谷歌开发者网站上,我找到了实现发送器和样式媒体接收器应用程序的描述。

我已经完成了此示例,它工作正常。我可以将托管在Google云端硬盘上的MP3文件投射到我的chromecast设备上。

现在,我已经实现了一个自定义接收器(见附件),它应该能够播放引用到 m3u8 文件的 URL。为此,我正在按照Google的建议使用媒体播放器库。

<body>
<div>
<p id='text'> </p>
<video id='vid'> </video>
</div>
<script type="text/javascript" src="https://www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/cast/sdk/libs/mediaplayer/1.0.0/media_player.js"></script>

<script type="text/javascript">
// If you set ?Debug=true in the URL, such as a different App ID in the
// developer console, include debugging information.
if (window.location.href.indexOf('Debug=true') != -1) {
    cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.DEBUG);
    cast.player.api.setLoggerLevel(cast.player.api.LoggerLevel.DEBUG);
}
console.log("mediaElement set");
var mediaElement = document.getElementById('vid');
// Create the media manager. This will handle all media messages by default.
window.mediaManager = new cast.receiver.MediaManager(mediaElement);
// Remember the default value for the Receiver onLoad, so this sample can Play
// non-adaptive media as well.
window.defaultOnLoad = mediaManager.onLoad;
mediaManager.onLoad = function (event) {
    // The Media Player Library requires that you call player unload between
    // different invocations.
    if (window.player !== null) {
        player.unload();    // Must unload before starting again.
        window.player = null;
    }
    // This trivial parser is by no means best practice, it shows how to access
    // event data, and uses the a string search of the suffix, rather than looking
    // at the MIME type which would be better.  In practice, you will know what
    // content you are serving while writing your player.

    if (event.data['media'] && event.data['media']['contentId']) {
    console.log('Starting media application');

    var t = document.getElementById('text');
    t.innerHTML = event.data['media'];
    console.log("EventData: "+event.data);
    console.log("EventData-Media: "+event.data['media']);
    console.log("EventData-ContendID: "+event.data['media']['contentId']);
    var url = event.data['media']['contentId'];
    console.log("URL: "+url);
    // Create the Host - much of your interaction with the library uses the Host and
    // methods you provide to it.
    window.host = new cast.player.api.Host(
        {'mediaElement':mediaElement, 'url':url});
    var ext = url.substring(url.lastIndexOf('.'), url.length);
    var initStart = event.data['media']['currentTime'] || 0;
    var autoplay = event.data['autoplay'] || true;
    var protocol = null;
    mediaElement.autoplay = autoplay;  // Make sure autoplay get's set
    protocol = cast.player.api.CreateHlsStreamingProtocol(host);
    host.onError = function(errorCode) {
      console.log("Fatal Error - "+errorCode);
      if (window.player) {
        window.player.unload();
        window.player = null;
      }
    };
    // If you need cookies, then set withCredentials = true also set any header
    // information you need.  If you don't need them, there can be some unexpected
    // effects by setting this value.
    //      host.updateSegmentRequestInfo = function(requestInfo) {
    //        requestInfo.withCredentials = true;
    //      };
    console.log("we have protocol "+ext);
    if (protocol !== null) {
      console.log("Starting Media Player Library");
      window.player = new cast.player.api.Player(host);
      window.player.load(protocol, initStart);
    }
    else {
      window.defaultOnLoad(event);    // do the default process
    }
  }   
}
window.player = null;
console.log('Application is ready, starting system');
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
castReceiverManager.start();
</script>
</body>

我已经发现,可以使用媒体播放器库投射.m3u8,.ism和.mpd文件。所以我创建了一个 m3u8 文件,如下所示,将其托管到 Google 云端硬盘,并尝试将其投射到我的自定义接收器。

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=173952
https://www.googledrive.com/host/0B1x31lLRAxTMRndtNkhSWVdGLVE

但它不起作用。我使用 Goolge Cast 开发人员控制台来调试自定义接收器。通过超越

window.player.load(protocol, initStart);

命令,我在控制台上收到致命错误。

我认为问题出在自定义接收器代码上,因为谷歌文档中的发送器应用程序与样式媒体接收器配合使用正常。

有没有人知道这个问题或在自定义接收器代码上看到一些问题?有谁知道我如何调试样式的媒体播放器?如果我可以看到哪些消息与样式媒体播放器交换,那会容易得多,但是如果我激活调试,我看不到交换的消息。

如果打开调试,则可以看到消息交换(请参阅此处的"调试"部分下)。在我们的 github 存储库中也有一个完整的接收器示例项目。

相关内容

  • 没有找到相关文章

最新更新