Search code examples
angularjshtml5-videovideogular

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


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 only one video at a time.

The system should automatically stop other video that is playing in background and play the new one

Thanks


Solution

  • 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();
                        }
                    }
                }
            };
        }
    );