为chromecast设置cors标头

时间:2014-08-23 01:07:57

标签: android chromecast

如何在Chromecast中为M3U8文件流设置CORS标头?在我的发件人(Android)中,我正在设置元数据和MediaInfo,如下所示:

metaData = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
metaData.putString(MediaMetadata.KEY_TITLE, "Demo Video");

MediaInfo mediaInfo = new MediaInfo.Builder(
        "http://playertest.longtailvideo.com/adaptive/bbbfull/bbbfull.m3u8")
        .setContentType("application/vnd.apple.mpegurl")
        .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
        .setMetadata(metaData)
        .build();

player.load(client, mediaInfo, true)
      .setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
       @Override
       public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {
                    Status status = mediaChannelResult.getStatus();
                                 if (status.isSuccess()) {
                                  }
                                  }
                                  });

我的onLoad方法设置如下:

mediaManager.onLoad = function(event) {
        console.log("### Media Manager - LOAD: " + JSON.stringify(event));

        if(mediaPlayer !== null) {
            mediaPlayer.unload(); // Ensure unload before loading again
        }

        if (event.data['media'] && event.data['media']['contentId']) {
            var url = event.data['media']['contentId'];

            mediaHost = new cast.player.api.Host({
                'mediaElement': mediaElement,
                'url': url
            });

            mediaHost.onError = function (errorCode) {
                console.error('### HOST ERROR - Fatal Error: code = ' + errorCode);

                if (mediaPlayer !== null) {
                    mediaPlayer.unload();
                }
            }

            var initialTimeIndexSeconds = event.data['media']['currentTime'] || 0;
            // TODO: real code would know what content it was going to access and this would not be here.
            var protocol = null;

            var parser = document.createElement('a');
            parser.href = url;

            var ext = ext = parser.pathname.split('.').pop();
            if (ext === 'm3u8') {
                protocol =  cast.player.api.CreateHlsStreamingProtocol(mediaHost);
            } else if (ext === 'mpd') {
                protocol = cast.player.api.CreateDashStreamingProtocol(mediaHost);
            } else if (ext === 'ism/') {
                protocol = cast.player.api.CreateSmoothStreamingProtocol(mediaHost);
            }
            console.log('### Media Protocol Identified as ' + ext);


            if (protocol === null) {

                mediaManager['onLoadOrig'](event); // Call on the original callback
            } else {

                mediaPlayer = new cast.player.api.Player(mediaHost);
                mediaPlayer.load(protocol, initialTimeIndexSeconds);
            }
        }
    }

但是,我收到此错误:

XMLHttpRequest cannot load http://playertest.longtailvideo.com/adaptive/bbbfull/bbbfull.m3u8. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '----' is therefore not allowed access. 

对于Chromecast,如何为Chromecast设置CORS标头?

1 个答案:

答案 0 :(得分:1)

可能为时已晚,但只是遇到同样的问题,并通过下面提到的方法完成。

我没有办法在发送方应用端添加标题,因此,分享我自己的经验。我首先确认我的服务器支持CORS,因此修复了CORS问题。然后,为了在chromecast上播放媒体,我需要在服务器上添加 gstatic.com和我的情况下另外一个作为允许的域,这实际上是CORS的整个想法,我们的服务器应该知道每个域。就是这样。

注意:非常确定go through this official documentation.但是作为初学者,从这里抓住所有东西可能看起来有点棘手。所以也分享了自己的经验。