问题
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