(function () { 'use strict'; angular.module('shared') .component('audioPlayer', { templateUrl: '/shared/js/angular/util/audio-player.html', controller: 'AudioPlayerController', bindings: { src: '<', text: '<' } }) .controller('AudioPlayerController', ['mediaPlayerRegistration', '$scope', function (mediaPlayerRegistration, $scope) { var ctrl = this; ctrl.$onInit = function () { ctrl.isSoundPlaying = false; ctrl.audio = new Audio(ctrl.src); }; ctrl.playPauseAudio = function () { if (ctrl.audio.paused) { // Ensure only one audio player plays at a time var lastPlayedAudioCtrl = mediaPlayerRegistration.getLastPlayedAudioCtrl(); if (lastPlayedAudioCtrl) { lastPlayedAudioCtrl.audio.pause(); lastPlayedAudioCtrl.isSoundPlaying = false; } var playPromise = ctrl.audio.play(); if (playPromise) { ctrl.isSoundPlaying = true; playPromise.then(function() { ctrl.isSoundPlaying = true; mediaPlayerRegistration.setLastPlayedAudioCtrl(ctrl); }).catch(function() { ctrl.isSoundPlaying = false; console.error("Error with playback of " + ctrl.src); }) } else { // playPromise is undefined in IE, still allow the sound to play ctrl.isSoundPlaying = true; mediaPlayerRegistration.setLastPlayedAudioCtrl(ctrl); } } else { ctrl.pauseAudio(); } ctrl.audio.addEventListener("ended", function () { ctrl.isSoundPlaying = false; $scope.$apply(); }); }; ctrl.pauseAudio = function () { ctrl.audio.pause(); ctrl.isSoundPlaying = false; }; ctrl.$onChanges = function () { ctrl.audio = new Audio(ctrl.src); }; ctrl.$onDestroy = function () { ctrl.isSoundPlaying = false; if (ctrl.audio) { ctrl.pauseAudio(); } } }]); })();