forked from audiosocket/backbone.soundmanager2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.soundmanager2.coffee
336 lines (260 loc) · 7.72 KB
/
backbone.soundmanager2.coffee
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
if require?
Backbone = require "backbone"
else
Backbone = window.Backbone
# A player model for use with SoundManager2 API. Requires soundmanager2 (duh).
class BackboneSoundManager2
_.extend this.prototype, Backbone.Events
# Create a new Player instance. There will probably only ever be one
# of these in the app. If `options.bus` is provided, rebroadcast all events
# to it with a `player:` prefix. Can also accept an 'autoPlay' option if
# you want the sound to start playing automatically on load.
#
# Also rebroadcast messages on the playable, if possible.
#
# Examples
#
# track = new Track()
# track.on "player:heyo", myFunction
#
# player = new Player()
# player.playable = track
# player.trigger "heyo"
# # => Will execute track.myFunction()
#
# Returns self.
constructor: (options = {}) ->
@options = options
if @options.bus?
@on "all", (event, args...) ->
@options.bus.trigger "player:#{event}", args...
@on "all", (event, args...) ->
return unless @playable?.trigger?
@playable.trigger "player:#{event}", args...
this
# Release the current sound, clear the current playable, and trigger
# a `released` event.
#
# Examples
#
# App.player.release() # => Playable is removed from player.
#
# Returns newly changed self.
release: ->
if @sound
@fadeout =>
@sound.destruct()
@sound = null
@trigger "releasing"
@playable.release() if @playable?.release?
@playable = null
@trigger "released"
this
# Fade out the current sound's volume to 0 and run any callbacks.
#
# Examples
#
# player.fadeout() # => volume smoothly goes to zero.
#
# Returns self
fadeout: (callback) ->
# Fade out started causing issues when the track sharing went in.
# Removing this ability for now until we can circle back.
callback()
return this
# Original fade out. Can't get here now.
s = @sound
vol = @volume * 100
fnc = =>
vol -= 2
s.setVolume vol
if vol > 0
_.delay fnc, 10
else
callback()
fnc()
this
# Determine if the state of the player is `paused`, or `playing`.
#
# Examples
#
# player.toggle track
# player.getState() # => "playing"
#
# Returns String indicating `paused` or `playing` state.
# Returns null if nothing is currently playing.
getState: ->
return unless @playable?
return "loading" unless @sound?
return "stopped" if @sound.playState == 0
if @sound.paused then "paused" else "playing"
# Determine if current playable is equal to playable passed in.
#
# Examples
#
# player.load track
# player.isAlreadyPlaying track # => true
#
# Returns boolean.
isAlreadyPlaying: (playable) ->
@playable? and @playable.id == playable.id
# Determine if a sound has already been already loaded.
#
# Examples
#
# player.load track
# player.hasSoundLoaded() # => true
#
# Returns boolean.
hasSoundLoaded: ->
@sound?
# Test if a `playable` has getAudioURL() method.
#
# playable - A model that fulfills the contract of having getAudioURL()
#
# Examples
#
# track = new Track();
# track.isPlayable() # => true
#
# Returns boolean.
isPlayable: (playable) ->
playable.getAudioURL?
ok: ->
soundManager.ok()
# Load a `playable` and create an SM2 sound instance (@sound) to represent it.
# Triggers `loading` and `loaded` events. Will also automatically play
# it's `playable` model.
#
# playable - A model that fulfills the contract of having getAudioURL()
#
# Examples
#
# track = new Track();
# player.load track
load: (playable) ->
# No need to load the same playable track
return if playable is @playable
unless soundManager.ok()
return @trigger "error", "SoundManager2 isn't ready."
unless @isPlayable playable
return @trigger "error",
"Playable doesn't satisfy the contract.", playable
@release()
@playable = playable
@playable.retain() if @playable.retain?
@trigger "loading"
playable.getAudioURL (url) =>
# It may happen that the playable has
# changed while fetching url..
return unless @playable == playable
@sound = soundManager.createSound
autoPlay : false # Trick: we want the "played" event
# to be emitted after the "loaded"
# event, so we call `@sound.play()`
# manually below..
id : playable.id
url : url
volume : Math.round @volume*100
onfinish : => @trigger "finished", @sound
onplay : => @trigger "played", @sound
onpause : => @trigger "paused", @sound
onresume : => @trigger "resumed", @sound
onstop : => @trigger "stopped", @sound
onconnect : => @trigger "connected", @sound
onid3 : => @trigger "id3data", @sound
whileplaying : => @trigger "playing", @sound
whileloading : => @trigger "bytesLoaded", @sound
@trigger "loaded", @sound
@sound.play() if @options.autoPlay is true
# Initial volume
volume: 1
# Set sound to be a particular volume, accepts values
# between 0. and 1.
#
# volume - a float between 0 and 1.
#
# Examples
#
# player.sound.volume # => 80
# player.setVolume 1
# player.sound.volume # => 100
#
# Returns SoundManager2 sound.
setVolume: (volume) ->
return unless @sound?
return if volume > 1 || volume < 0
@volume = volume
@sound.setVolume Math.round(@volume * 100)
@sound
# Move to a specific position in the current sound. `position` is a
# percentage, expressed as a number between 0 and 1.
#
# position - a float between 0 and 1.
#
# Examples
#
# # for a track with a duration of 5000 milliseconds
# player.sound.position # => 1000
# player.setPosition 0.5
# player.sound.position # => 2500
#
# Returns SoundManager2 sound.
setPosition: (position) ->
return unless @sound?
return if @sound.bytesLoaded / @sound.bytesTotal < position
@sound.setPosition position * @sound.durationEstimate
@sound
# Move to a position relative to current position, in milliseconds
#
# position - a integer..
#
# Examples
#
# # For a track with a duration of 5000 milliseconds
# player.sound.position # => 1000
# player.setRelativePosition -500
# player.sound.position # => 500
setRelativePosition: (position) ->
return unless @sound?
@sound.setPosition @sound.position + position
# Toggle play/pause for the current sound or load a new `playable`.
#
# playable - A model that fulfills the contract of having getAudioURL()
#
# Examples
#
# player.toggle track # => track will load and start playing
toggle: (playable = @playable) ->
if @sound? and @isAlreadyPlaying playable
@sound.togglePause()
else if playable?
@stop()
@load playable
# Start playing the current sound.
#
# Examples
#
# player.play() # => track will start playing
play: ->
return if ! @sound? or @getState is 'playing'
@sound.play()
# Stop for the current sound.
#
# Examples
#
# player.stop() # => track will stop completely
stop: ->
return unless @sound?
@sound.stop()
pause: ->
return unless @sound
@fadeout =>
@sound.pause()
@sound.setVolume 100
# If we are in Node environment — export our constructor function in a module,
# else — add it to Backbone object as a property
if module? and module.exports?
module.exports = BackboneSoundManager2
else
Backbone.SoundManager2 = BackboneSoundManager2