(function () { 'use strict'; angular.module('shared') .service('mediaPlayerRegistration', [mediaPlayerRegistrationService]); function mediaPlayerRegistrationService() { var self = this; // Remember the last played audio file so we can ensure only one audio file plays at a time var lastPlayedAudioCtrl = null; self.pauseAllMedia = function () { if (lastPlayedAudioCtrl) { lastPlayedAudioCtrl.pauseAudio(); } if (angular.element('video')[0]) { angular.element('video')[0].pause(); } }; self.playAudio = function(newPlayingAudioCtrl) { // Ensure only one audio player plays at a time if (lastPlayedAudioCtrl) { lastPlayedAudioCtrl.pauseAudio(); } var playPromise = newPlayingAudioCtrl.audio.play(); if (playPromise) { newPlayingAudioCtrl.isSoundPlaying = true; playPromise.then(function() { newPlayingAudioCtrl.isSoundPlaying = true; lastPlayedAudioCtrl = newPlayingAudioCtrl; }).catch(function() { newPlayingAudioCtrl.isSoundPlaying = false; console.error("Error with playback of " + newPlayingAudioCtrl.src); }) } else { // playPromise is undefined in IE, still allow the sound to play newPlayingAudioCtrl.isSoundPlaying = true; lastPlayedAudioCtrl = newPlayingAudioCtrl; } } } })();