stop other video that is playing in background on play the new one

半城伤御伤魂 提交于 2019-11-28 02:13:34

You can get all the APIs separately for each player and listen for a change in the state:

<videogular ng-repeat="config in ctrl.videoConfigs" vg-player-ready="ctrl.onPlayerReady($API, $index)" vg-update-state="ctrl.onUpdateState($state, $index)">
    <!-- other plugins here -->
</videogular>

In you controller:

'use strict';
angular.module('myApp').controller('MainCtrl',
    function ($sce) {
        // this.videoConfigs should have different configs for each player...

        this.players = [];

        this.onPlayerReady = function (API, index) {
            this.players[index] = API;
        };

        this.onUpdateState = function (state, index) {
            if (state === 'play') {
                // pause other players
                for (var i=0, l=this.players.length; i<l; i++) {
                    if (i !== index) {
                        this.players[i].pause();
                    }
                }
            }
        };
    }
);

While the answer from elecash is a fine answer, there is a lot of guessing and storage. I choose to store only the active api on the $rootScope and paused the other player when a new player has started.

<videogular vg-player-ready="playerReady($API)" vg-update-state="stateChange($state)">
</videogular>

controller('VideoPlayerCtrl', ['$rootScope','$scope', function($rootScope,$scope) {
  $scope.playerReady = function(api) {
    $scope.api = api;
  };

  $scope.stateChange = function(state) {
    if(state=='play') {
      if($rootScope.playingVideo && $rootScope.playingVideo != $scope.api) $rootScope.playingVideo.pause();
      $rootScope.playingVideo = $scope.api;
    }
  };
}]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!