~ken-vandine/+junk/hangonman

« back to all changes in this revision

Viewing changes to www/js/soundboard.js

  • Committer: Ken VanDine
  • Date: 2014-03-07 03:22:47 UTC
  • Revision ID: ken.vandine@canonical.com-20140307032247-uk4i0eqyf4l7e4em
initial packaging on hangonman

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2013, Intel Corporation.
 
3
 *
 
4
 * This program is licensed under the terms and conditions of the
 
5
 * Apache License, version 2.0.  The full text of the Apache License is at
 
6
 * http://www.apache.org/licenses/LICENSE-2.0
 
7
 *
 
8
 */
 
9
 
 
10
// files is a map from a sound alias to a file URL,
 
11
// e.g. {"backgroundSound": "audio/background.ogg", ...}
 
12
var SoundBoard = function (files) {
 
13
  this.files = files;
 
14
  this.muted = false;
 
15
  this.sounds = {};
 
16
};
 
17
 
 
18
SoundBoard.prototype.load = function (name) {
 
19
  if (!this.sounds[name]) {
 
20
    this.sounds[name] = new Sound(this.files[name]);
 
21
  }
 
22
  return this.sounds[name];
 
23
};
 
24
 
 
25
// pause all playing sounds
 
26
SoundBoard.prototype.pause = function () {
 
27
  for (var name in this.sounds) {
 
28
    this.sounds[name].pause();
 
29
  }
 
30
};
 
31
 
 
32
// play one sound
 
33
SoundBoard.prototype.play = function (name) {
 
34
  if (this.muted) {
 
35
    return;
 
36
  }
 
37
 
 
38
  var sound = this.load(name);
 
39
 
 
40
  if (!sound.ready) {
 
41
    sound.on('ready', function () {
 
42
      sound.play();
 
43
      sound.off('ready');
 
44
    });
 
45
  }
 
46
  else {
 
47
    sound.play();
 
48
  }
 
49
};