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

时间:2014-11-13 14:35:29

标签: android casting chromecast google-cast custom-receiver

我想在我的自定义接收器上实现媒体播放器功能。 在Google开发者网站上,我找到了实施senderstyled media receiver申请的说明。

我已经完成了这个样本,它运行正常。我可以将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>

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

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

但它不起作用。我使用Goolge Cast Developer Console来调试自定义接收器。通过排除

window.player.load(protocol, initStart);

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

我认为问题在于自定义接收器代码,因为Google文档中的发件人应用程序可以与样式化的媒体接收器一起使用。

是否有人知道此问题或在自定义接收器代码上看到一些问题?有谁知道如何调试风格的媒体播放器?如果我能看到哪些消息与样式化的媒体播放器交换会更容易,但如果我激活调试我看不到交换的消息。

1 个答案:

答案 0 :(得分:0)

如果打开调试,您可以看到消息交换(请参阅调试部分下的here)。我们的github回购中还有一个完整的接收器sample项目。