3

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. How can I stop the YouTube video playing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Roxx
  • 3,738
  • 20
  • 92
  • 155
  • Have you already tried to put this pause-event-listener into a "deviceready"-listener? Like in [this](https://cordova.apache.org/docs/en/latest/cordova/events/events.html) example. – Blauharley Aug 20 '17 at 11:33
  • Another issue is that you try to access the rootscope outside angular/ionic: You can get the root-scope via [angular.element](https://stackoverflow.com/a/24596251). – Blauharley Aug 20 '17 at 15:29

0 Answers0