Как приостановить или остановить приложение Ionic YouTube, играющее в фоновом режиме - PullRequest
0 голосов
/ 03 сентября 2018

Я хочу прекратить воспроизведение видео с YouTube в фоновом режиме, когда нажимаю кнопки «Домой» и блокировки экрана на телефоне.

Я использую iframe для загрузки видео на YouTube:

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

Я использовал приведенный ниже код для остановки, но это не удалось:

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

Также, как я могу добиться нажатия кнопки блокировки при нажатии на моем устройстве?

Ответы [ 3 ]

0 голосов
/ 23 сентября 2018
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>
0 голосов
/ 22 января 2019

Для ионной версии 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;
            });
0 голосов
/ 03 сентября 2018

Вы можете сделать это через 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> 

Вы также можете преобразовать эту треску в угловую службу и использовать ее.

для получения дополнительной информации см .: https://developers.google.com/youtube/iframe_api_reference#Getting_Started

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...