【问题标题】:Creating a custom receiver with the Google Cast - Media Player Library使用 Google Cast - 媒体播放器库创建自定义接收器
【发布时间】:2015-01-10 17:48:04
【问题描述】:

我想为我的自定义接收器实现媒体播放器功能。 在google开发者网站上,我找到了实现senderstyled media receiver应用程序的描述。

我已经完成了这个示例,并且效果很好。我可以将托管在 Google Drive 上的 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 Drive,并尝试将其投射到我的自定义接收器。

#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);

命令,我在控制台上收到一个 FATAL ERROR。

我认为问题出在自定义接收器代码上,因为来自 google 文档的发送器应用程序可以与样式化媒体接收器一起正常工作。

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

【问题讨论】:

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


    【解决方案1】:

    如果您打开调试,您可以看到消息交换(请参阅“调试”部分下的here)。在我们的 github 仓库中也有一个成熟的接收器 sample 项目。

    【讨论】:

    • 非常感谢您的快速答复。通过引用的示例,我能够解决我的问题。谢谢!
    猜你喜欢
    • 2021-12-10
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2014-09-03
    相关资源
    最近更新 更多