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) {
43 var url = service.url_cache[path] ||
44 service.base_url + path.replace(/\./g, '/') + '.wav';
46 var player = new Audio(url);
48 player.onloadeddata = function() {
49 console.debug('Playing audio URL: ' + url);
50 service.url_cache[orig_path] = url;
54 if (service.url_cache[path]) {
55 // when serving from the cache, avoid secondary URL lookups.
59 player.onerror = function() {
60 // Unable to play path at the requested URL.
62 if (!path.match(/\./)) {
63 // all fall-through options have been exhausted.
66 "No suitable URL found for path '" + orig_path + "'");
70 // Fall through to the next (more generic) option
71 path = path.replace(/\.[^\.]+$/, '');
72 service.play_url(path, orig_path);