【问题标题】:How to pause or stop ionic youtube app playing in the background如何暂停或停止 ionic youtube 应用程序在后台播放
【发布时间】:2018-09-03 17:35:53
【问题描述】:

当我按下手机上的主页和屏幕锁定按钮时,我想停止在后台播放 youtube 视频。

我正在使用 iframe 加载 youtube 视频:

<iframe src="https://www.youtube.com/embed/gvI2ClWqHO0" frameborder="0" width="560" height="315"></iframe>

我使用下面的代码停止但它失败了:

ionViewWillLeave() {
this.platform.exitApp();
}

另外,当我从我的设备上按下锁定按钮时,如何实现点击锁定按钮?

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    你可以通过JAVASCRIPT来做:

    <!DOCTYPE html>
    <html>
      <body>
        <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
        <div id="player"></div>
    
        <script>
          // 2. This code loads the IFrame Player API code asynchronously.
          var tag = document.createElement('script');
    
          tag.src = "https://www.youtube.com/iframe_api";
          var firstScriptTag = document.getElementsByTagName('script')[0];
          firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
          // 3. This function creates an <iframe> (and YouTube player)
          //    after the API code downloads.
          var player;
          function onYouTubeIframeAPIReady() {
            player = new YT.Player('player', {
              height: '390',
              width: '640',
              videoId: 'M7lc1UVf-VE',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
          }
    
          // 4. The API will call this function when the video player is ready.
          function onPlayerReady(event) {
            event.target.playVideo();
          }
    
          // 5. The API calls this function when the player's state changes.
          //    The function indicates that when playing a video (state=1),
          //    the player should play for six seconds and then stop.
          var done = false;
          function onPlayerStateChange(event) {
            if (event.data == YT.PlayerState.PLAYING && !done) {
              setTimeout(stopVideo, 6000);
              done = true;
            }
          }
          function stopVideo() {
            player.stopVideo();
          }
        </script>
      </body>
    </html> 
    

    您还可以将该 cod 转换为 Angular 服务并使用它。

    更多信息,请参阅:https://developers.google.com/youtube/iframe_api_reference#Getting_Started

    【讨论】:

    • 我尝试了来自 '@ionic-native/insomnia' 的 { Insomnia };。成功了
    【解决方案2】:
    I tried with Ionic Insomnia and it worked :
    
    <code>
    import { Insomnia } from '@ionic-native/insomnia';
    
    constructor(private insomnia: Insomnia) { }
    
    ...
    
    this.insomnia.keepAwake()
      .then(
        () => console.log('success'),
        () => console.log('error')
      );
    
    </code>
    

    【讨论】:

      【解决方案3】:

      对于 Ionic 版本 1

      1. 在里面添加

        app.run(function(){
        document.addEventListener('deviceready', function () {
                    // cordova.plugins.backgroundMode is now available
                    console.log("Device ready !!!");
                }, false);
        
        
            document.addEventListener("pause", function () {
                if ($rootScope.YTPlayer) {
                    $rootScope.YTPlayer.stopVideo();
                }
            }, false);
        

        })

      然后添加控制器

      $scope.$on('youtube.player.ready', function ($event, player) {
                      $rootScope.YTPlayer = player;
                  });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-30
        • 2013-09-25
        • 2015-03-20
        相关资源
        最近更新 更多