audio.service.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * AccessibleBlockly
  3. *
  4. * Copyright 2016 Google Inc.
  5. * https://developers.google.com/blockly/
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the 'License');
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an 'AS IS' BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * @fileoverview Angular2 Service that plays audio files.
  21. * @author sll@google.com (Sean Lip)
  22. */
  23. blocklyApp.AudioService = ng.core
  24. .Class({
  25. constructor: [function() {
  26. // We do not play any audio unless a media path prefix is specified.
  27. this.canPlayAudio = false;
  28. if (ACCESSIBLE_GLOBALS.hasOwnProperty('mediaPathPrefix')) {
  29. this.canPlayAudio = true;
  30. var mediaPathPrefix = ACCESSIBLE_GLOBALS['mediaPathPrefix'];
  31. this.AUDIO_PATHS_ = {
  32. 'connect': mediaPathPrefix + 'click.mp3',
  33. 'delete': mediaPathPrefix + 'delete.mp3'
  34. };
  35. }
  36. // TODO(sll): Add ogg and mp3 fallbacks.
  37. this.cachedAudioFiles_ = {};
  38. }],
  39. play_: function(audioId) {
  40. if (this.canPlayAudio) {
  41. if (!this.cachedAudioFiles_.hasOwnProperty(audioId)) {
  42. this.cachedAudioFiles_[audioId] = new Audio(
  43. this.AUDIO_PATHS_[audioId]);
  44. }
  45. this.cachedAudioFiles_[audioId].play();
  46. }
  47. },
  48. playConnectSound: function() {
  49. this.play_('connect');
  50. },
  51. playDeleteSound: function() {
  52. this.play_('delete');
  53. }
  54. });