2014-01-03 13:32:13 -05:00
|
|
|
CocoClass = require 'lib/CocoClass'
|
|
|
|
AudioPlayer = require 'lib/AudioPlayer'
|
|
|
|
{me} = require 'lib/auth'
|
|
|
|
|
|
|
|
CROSSFADE_LENGTH = 1500
|
|
|
|
|
|
|
|
module.exports = class MusicPlayer extends CocoClass
|
|
|
|
currentMusic: null
|
|
|
|
standingBy: null
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
subscriptions:
|
2014-08-27 15:24:03 -04:00
|
|
|
'music-player:play-music': 'onPlayMusic'
|
2014-01-03 13:32:13 -05:00
|
|
|
'audio-player:loaded': 'onAudioLoaded'
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
constructor: ->
|
2014-02-12 15:41:41 -05:00
|
|
|
super arguments...
|
|
|
|
me.on 'change:music', @onMusicSettingChanged, @
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onAudioLoaded: ->
|
|
|
|
@onPlayMusic(@standingBy) if @standingBy
|
|
|
|
|
|
|
|
onPlayMusic: (e) ->
|
|
|
|
src = e.file
|
2014-05-20 16:43:12 -04:00
|
|
|
src = "/file#{e.file}#{AudioPlayer.ext}"
|
|
|
|
if (not e.file) or src is @currentMusic?.src
|
|
|
|
if e.play then @restartCurrentMusic() else @fadeOutCurrentMusic()
|
|
|
|
return
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-05-20 16:43:12 -04:00
|
|
|
media = AudioPlayer.getStatus(src)
|
|
|
|
if not media?.loaded
|
|
|
|
AudioPlayer.preloadSound(src)
|
|
|
|
@standingBy = e
|
|
|
|
return
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@standingBy = null
|
2014-05-20 16:43:12 -04:00
|
|
|
@fadeOutCurrentMusic()
|
|
|
|
@startNewMusic(src) if e.play
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-05-20 16:43:12 -04:00
|
|
|
restartCurrentMusic: ->
|
|
|
|
return unless @currentMusic
|
|
|
|
@currentMusic.play('none', 0, 0, -1, 0.3)
|
|
|
|
@updateMusicVolume()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-05-20 16:43:12 -04:00
|
|
|
fadeOutCurrentMusic: ->
|
|
|
|
return unless @currentMusic
|
|
|
|
f = -> @stop()
|
2014-06-30 22:16:26 -04:00
|
|
|
createjs.Tween.get(@currentMusic).to({volume: 0.0}, CROSSFADE_LENGTH).call(f)
|
|
|
|
|
2014-05-20 16:43:12 -04:00
|
|
|
startNewMusic: (src) ->
|
|
|
|
@currentMusic = createjs.Sound.play(src, 'none', 0, 0, -1, 0.3) if src
|
2014-01-03 13:32:13 -05:00
|
|
|
return unless @currentMusic
|
|
|
|
@currentMusic.volume = 0.0
|
|
|
|
if me.get('music')
|
2014-06-30 22:16:26 -04:00
|
|
|
createjs.Tween.get(@currentMusic).to({volume: 1.0}, CROSSFADE_LENGTH)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onMusicSettingChanged: ->
|
|
|
|
@updateMusicVolume()
|
|
|
|
|
|
|
|
updateMusicVolume: ->
|
|
|
|
return unless @currentMusic
|
|
|
|
createjs.Tween.removeTweens(@currentMusic)
|
|
|
|
@currentMusic.volume = if me.get('music') then 1.0 else 0.0
|
2014-02-12 15:41:41 -05:00
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
me.off 'change:music', @onMusicSettingChanged, @
|
2014-02-14 13:57:47 -05:00
|
|
|
super()
|