-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm2.js
151 lines (136 loc) · 2.97 KB
/
sm2.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
// Basic usage
sm2.initialize({
url: 'http://www.example.org/path/to/soundmanager2/swf/'
});
sm2.play('/path/to/sound.mp3');
sm2.pause('/path/to/sound.mp3');
// Enable looping
var loop = true;
sm2.play('/path/to/sound.mp3', loop);
// Toggle play/pause
var playing = sm2.toggle('/path/to/sound.mp3');
if (playing) {
// update UI
} else {
// update UI
}
// Play and pause methods return a SoundManager2 sound object
var sound = sm2.play('/path/to/sound.mp3');
sound.onid3 = function(id3) {
// handle id3 info
};
// Set a callback when the sound finishes playing
// Note: the second argument can either be a boolean (to control looping) or
// a function (treated as a callback)
sm2.play('/path/to/mp3', function() {
// do something here
});
// Using a playlist
var playlist = [
'/path/to/1.mp3',
'/path/to/2.mp3',
'/path/to/3.mp3'
];
var position = 0;
function play_next() {
pause_current();
position = (position + 1) % playlist.length;
play_current();
}
function callback() {
play_next();
}
function play_current() {
sm2.play(playlist[position], callback);
}
function pause_current() {
sm2.pause(playlist[position]);
}
function toggle_current() {
sm2.toggle(playlist[position], callback);
}
*/
var sm2 = {
sounds: {},
ready: false,
queue: [],
count: 0,
defaults: {
url: '/soundmanager2/swf/',
flashVersion: 9,
useFlashBlock: true,
consoleOnly: true,
bgColor: '#FFFFFF'
},
initialize: function(options) {
if (!options) {
options = {};
}
for (var key in this.defaults) {
soundManager[key] = this.defaults[key];
}
for (var key in options) {
soundManager[key] = options[key];
}
var self = this;
soundManager.onready(function() {
self.ready = true;
if (self.queue) {
for (var i = 0; i < self.queue.length; i++) {
self.play(self.queue[i]);
}
self.queue = [];
}
});
soundManager.ontimeout(function() {
alert('Oops, there was a problem playing sound!');
});
},
play: function(url, option) {
if (!this.ready) {
this.queue.push(url);
return false;
}
var sound = this.sounds[url];
if (!sound) {
this.count++;
sound = soundManager.createSound({
id: 'sm2_' + this.count,
url: url,
onfinish: function() {
if (this.loop) {
this.play();
} else if (this.callback) {
this.callback.apply(this);
}
}
});
this.sounds[url] = sound;
}
if (option === true) {
sound.loop = true;
} else if (typeof option === 'function') {
sound.callback = option;
}
sound.play();
return sound;
},
pause: function(url) {
var sound = this.sounds[url];
if (!sound) {
return false;
}
sound.pause();
return sound;
},
toggle: function(url, option) {
if (!this.sounds[url] || this.sounds[url].paused || !this.sounds[url].playState) {
this.play(url, option);
return true;
} else {
this.pause(url);
return false;
}
}
};