How to disable YouTube background playing in Cordova Android?

那年仲夏 提交于 2019-12-11 08:09:34

问题


I am trying to stop the YouTube video playing when I lock my mobile.

I tried almost each solution provided in SO but issue still persist.

How to pause YouTube video on InAppBrowser when my Cordova app goes to background?

PhoneGap keep running on Android after onPause

How to disable youtube background playing in phonegap android

How to stop youtube video playing in Android webview?

My app got rejected from google play due to some issue with network policy?

How do I fix this: app enables background playing of YouTube videos

App enables background playing of videos, cordova issues, google play rejecting apps

I have added the below code in confix.xml.

<preference name="KeepRunning" value="false" />

HTML and Angular Code

  <script src="https://www.youtube.com/iframe_api"></script>
<!--    <script src="./js/angular-youtube-embed.min.js"></script> -->
    <script src="./js/angular-youtube-embed.js"></script>

Angular

var ProfileApp = angular.module('ProfileApp', ['youtube-embed']);
$scope.$on('youtube.player.ready', function ($event, player) {
  $rootScope.YTPlayer = player;
});

<div ng-style="{'display':youtubevideo == ''?'none':'block'}">  
                                            <iframe title="YouTube video player"
class="YouTube-player" type="text/HTML" width="350" height="194"
                                                    ng-src="{{getIframeSrc(youtubevideo)}}" frameborder="0" allowfullscreen></iframe></div>

<youtube-video video-url="youtubevideo" player-width="'350px'" player-height="'194'" allowfullscreen></youtube-video>

Some JS code to detect pause event.

 <script>
    document.addEventListener("pause", function() {
  if ($rootScope.YTPlayer) {
    $rootScope.YTPlayer.stopVideo();
   //or maybe $rootScope.YTPlayer.pauseVideo();
  }
}, false);
    </script>

You can see I tried both approach iframe and YouTube API but none of them are working. Please advise how can I stop the YouTube video playing.

来源:https://stackoverflow.com/questions/45775517/how-to-disable-youtube-background-playing-in-cordova-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!