~ken-vandine/+junk/hangonman

« back to all changes in this revision

Viewing changes to www/js/sound.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
var Sound = function (config) {
 
10
  config = config || {};
 
11
  config.url = config.url || '';
 
12
  config.loop = (config.loop === true ? true : false);
 
13
 
 
14
  var self = this;
 
15
  smokesignals.convert(this);
 
16
 
 
17
  this.ready = false;
 
18
  this.isPlaying = false;
 
19
 
 
20
  this.audio = new Audio();
 
21
 
 
22
  this.audio.addEventListener('canplaythrough', function () {
 
23
    var firstTime = !self.ready;
 
24
 
 
25
    self.ready = true;
 
26
 
 
27
    if (firstTime) {
 
28
      self.emit('ready');
 
29
    }
 
30
  });
 
31
 
 
32
  this.audio.addEventListener('pause', function () {
 
33
    self.isPlaying = false;
 
34
  });
 
35
 
 
36
  this.audio.addEventListener('play', function () {
 
37
    self.isPlaying = true;
 
38
  });
 
39
 
 
40
  // this is to work around Android xwalk not looping audio
 
41
  // when the loop attribute is set:
 
42
  // https://github.com/crosswalk-project/crosswalk/issues/659
 
43
  this.audio.addEventListener('ended', function () {
 
44
    self.isPlaying = false;
 
45
    if (config.loop) {
 
46
      self.play();
 
47
    }
 
48
  });
 
49
 
 
50
  this.audio.autoplay = false;
 
51
  this.audio.preload = 'auto';
 
52
  this.audio.src = config.url;
 
53
};
 
54
 
 
55
Sound.prototype.play = function () {
 
56
  if (!this.ready) {
 
57
    return;
 
58
  }
 
59
 
 
60
  if (!this.isPlaying) {
 
61
    this.audio.play();
 
62
  }
 
63
};
 
64
 
 
65
Sound.prototype.pause = function () {
 
66
  this.audio.pause();
 
67
};