2 * Core Service - egAudio
4 * Plays audio files by key name. Each sound uses a dot-path to indicate
8 * sound => 'warning.checkout.no_item'
9 * URLs are tested in the following order until a valid audio file is found
10 * or no other paths are left to check.
12 * /audio/notifications/warning/checkout/not_found.wav
13 * /audio/notifications/warning/checkout.wav
14 * /audio/notifications/warning.wav
16 * TODO: move audio file base path settings to the template
17 * for configurability?
19 * Files are only played when sounds are configured to play via
20 * workstation settings.
23 angular.module('egCoreMod')
25 .factory('egAudio', ['$q','egHatch', function($q, egHatch) {
28 url_cache : {}, // map key names to audio file URLs
29 base_url : '/audio/notifications/'
33 * Play the sound found at the requested string path. 'path' is a
34 * key name which maps to an audio file URL.
36 service.play = function(path) {
38 service.play_url(path, path);
41 service.play_url = function(path, orig_path) {
42 console.log('audio: play_url('+path+','+orig_path+')');
44 egHatch.getItem('eg.audio.disable').then(function(audio_disabled) {
45 if (!audio_disabled) {
47 var url = service.url_cache[path] ||
48 service.base_url + path.replace(/\./g, '/') + '.wav';
50 var player = new Audio(url);
52 player.onloadeddata = function() {
53 service.url_cache[orig_path] = url;
55 console.log('audio: ' + url);
58 if (service.url_cache[path]) {
59 // when serving from the cache, avoid secondary URL lookups.
63 player.onerror = function() {
64 // Unable to play path at the requested URL.
66 if (!path.match(/\./)) {
67 // all fall-through options have been exhausted.
70 "No suitable URL found for path '" + orig_path + "'");
74 // Fall through to the next (more generic) option
75 path = path.replace(/\.[^\.]+$/, '');
76 service.play_url(path, orig_path);