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

后端 未结 2 580
渐次进展
渐次进展 2020-12-07 04:54

I am using Videogular to show video. could you please help me how to stop/pause other video when user click on play button to new one. So by this at a time user can play onl

相关标签:
2条回答
  • 2020-12-07 05:07

    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();
                        }
                    }
                }
            };
        }
    );
    
    0 讨论(0)
  • 2020-12-07 05:18

    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;
        }
      };
    }]);
    
    0 讨论(0)
提交回复
热议问题