cirandas.net

ref: master

public/javascripts/vendor/jquery.sound.js


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * jQuery sound plugin (no flash)
 * 
 * port of script.aculo.us' sound.js (http://script.aculo.us), based on code by Jules Gravinese (http://www.webveteran.com/) 
 * 
 * Copyright (c) 2007 Jörn Zaefferer (http://bassistance.de) 
 * 
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *   
 * $Id$
 */

/**
 * API Documentation
 * 
 * // play a sound from the url
 * $.sound.play(url)
 * 
 * // play a sound from the url, on a track, stopping any sound already running on that track
 * $.sound.play(url, {
 *   track: "track1"
 * });
 * 
 * // increase the timeout to four seconds before removing the sound object from the dom for longer sounds
 * $.sound.play(url, {
 *   timeout: 4000
 * });
 * 
 * // disable playing sounds
 * $.sound.enabled = false;
 * 
 * // enable playing sounds
 * $.sound.enabled = true
 */

(function($) {
	
$.sound = {
  tracks: {},
  enabled: true,
  template: function(src) {
  	return '<embed style="height:0" loop="false" src="' + src + '" autostart="true" hidden="true"/>';
  },
  play: function(url, options){
    if (!this.enabled)
		return;
    var settings = $.extend({
	  url: url,
	  timeout: 2000
    }, options);
	
	if (settings.track) {
		if (this.tracks[settings.track]) {
			var current = this.tracks[settings.track];
			current.Stop && current.Stop();
			current.remove();  
		}
	}
	
	var element = $.browser.msie
	  	? $('<bgsound/>').attr({
	        src: settings.url,
			loop: 1,
			autostart: true
	      })
	  	: $(this.template(settings.url));
    element.appendTo("body");
	
	if (settings.track) {
		this.tracks[settings.track] = element;
	}
	
	setTimeout(function() {
		element.remove();
	}, 2000)
  }
};

})(jQuery);