Reputation: 553
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
Upvotes: 2
Views: 3553
Reputation: 302
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;
}
};
}]);
Upvotes: 1
Reputation: 925
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();
}
}
}
};
}
);
Upvotes: 6