代码之家  ›  专栏  ›  技术社区  ›  skjagini

YouTube嵌入的视频“不能在服务器上播放”,而应用程序“在本地主机上运行良好”

  •  -1
  • skjagini  · 技术社区  · 7 年前

    应用程序在本地主机上运行良好,在本地主机上可以播放所有视频,当部署到服务器上时,大多数示例视频都无法工作。

    由于这些视频既可以在本地主机上播放,也可以在YouTube网站上播放,所以我不确定这里缺少什么,在dev工具上也看不到任何警告。

    例如,这里是示例视频id:vlkNcHDFnGA,我还可以看到视频可以直接在iframe上播放: https://jsfiddle.net/skjagini/419wcuod/

    YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = "key",
                    ApplicationName = this.GetType().ToString()
                });
    
                var searchListRequest = youtubeService.Search.List("snippet");
                searchListRequest.Q = searchText; 
                searchListRequest.MaxResults = 5; // 50
                searchListRequest.Type = "video";
                searchListRequest.VideoSyndicated = SearchResource.ListRequest.VideoSyndicatedEnum.True__;
                searchListRequest.VideoEmbeddable = SearchResource.ListRequest.VideoEmbeddableEnum.True__;
    

    在.html文件中

    <section class="app-footer footer">
        <div class="container-fluid">
    
            <span class="float-left">
                <div class="player-defaults" id="video-player"></div>
            </span>
    

    在.ts文件中

    this.player = YouTubePlayer('video-player', {
        height: 60, width: 100
    });
    
    this.player.on('stateChange', (event) => {
        if (!this.stateNames[event.data]) {
            throw new Error('Unknown state (' + event.data + ').');
            // console.log()
        } else if (event.data === 0) {
            this.playNext();
        }
    });
    
    playVideoById(vidoeId: string) {
        if (vidoeId == null) {
            if (this.activeSong) {
                vidoeId = this.activeSong.videoId;
            } else {
                return;
            }
        }
        this.isPlaying = true;
        // 'loadVideoById' is queued until the player is ready to receive API calls.
        this.player.loadVideoById(vidoeId); // player.loadVideoById('M7lc1UVf-VE');
        this.playVideo();
    }
    
    playVideo() {
        this.isPlaying = true;
        // 'playVideo' is queue until the player is ready to received API calls and after 'loadVideoById' has been called.
        this.player.playVideo();
    }
    

    编辑:当我右击youtube播放器并选择embedded video时,它似乎生成了正确的html,这与我的JSFiddle相同

    <iframe width="1920" height="1080" src="https://www.youtube.com/embed/vlkNcHDFnGA" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
    

    如果我点击youtube的标志,它会在youtube浏览器中打开,播放视频就很好了。

    编辑: 我注意到我得到的是CORD(不是CORS错误),因为我的服务器是http,而youtube在幕后调用的google ads服务是https。这可能是个问题吗?

    2 回复  |  直到 7 年前
        1
  •  0
  •   skjagini    7 年前

    Youtube IFrame希望主机在https域上运行。

    IFrame在localhost上工作得很好,因为localhost被认为是安全的,但是当托管在公共域上时,它需要通过Https提供服务。

    我使用了来自LetsEncrypt的免费证书来获取域的ssl,此后一切都开始工作了。

        2
  •  -1
  •   Adi Mabfalin    7 年前

    YouTube嵌入的视频无法在服务器上播放

    我觉得很清楚。这意味着服务器不支持嵌入式视频。

    推荐文章