在Bootstrap模式中播放的AngularJS中禁用YouTube视频

时间:2014-10-23 23:40:12

标签: angularjs twitter-bootstrap bootstrap-modal video-embedding

我在Bootstrap模式中嵌入了一个YouTube视频。问题是,当用户点击远离模态时,它会关闭,视频会继续播放。我试图添加一些东西,我可以检查模态是否已关闭,然后停止视频。我曾尝试使用$scope.$watch('videoModalone',但没有运气。

          <div class="modal fade" id="videoModalone" ng-model="youtubeVideo">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title"></h4>
                        </div>
                        <div class="modal-body">
                            <iframe width="550" height="350" src="http://www.youtube.com/embed/mwuPTI8AT7M?rel=0" frameborder="0"></iframe>
                        </div>
                    </div>
                </div>
            </div>

2 个答案:

答案 0 :(得分:3)

对于那些感兴趣的人来说,这就是我的工作方式:

     <div class="modal fade" id="videoModalone">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title"></h4>
                    </div>
                    <div class="modal-body">
                        <iframe width="550" height="350" src="http://www.youtube.com/embed/mwuPTI8AT7M?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0"></iframe>
                    </div>
                </div>
            </div>
        </div>

然后在我的控制器中,我寻找模态结束

$('#videoModalone').on('hide.bs.modal', function () {
    var outerDiv = document.getElementById("videoModalone");
    var youtubeIframe = outerDiv.getElementsByTagName("iframe")[0].contentWindow;
    youtubeIframe.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*'); 
});

这就是诀窍现在所有这一切的关键是视频的网址 ?enablejsapi = 1&安培;版本= 3及playerapiid = YouTube播放

答案 1 :(得分:0)

对于Bootstrap 5 Modal

  ngAfterContentChecked(): void {
    document.getElementById('videoModalone').addEventListener('hide.bs.modal', function () {
      const getFrame = this.getElementsByTagName("iframe")[0].contentWindow;
      getFrame.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*'); 
    });
  }