159 lines
3.7 KiB
CoffeeScript
159 lines
3.7 KiB
CoffeeScript
$ = jQuery
|
|
context = window
|
|
logger = context.JK.logger
|
|
rest = context.JK.Rest()
|
|
EVENTS = context.JK.EVENTS
|
|
|
|
BrowserMediaActions = @BrowserMediaActions
|
|
|
|
@BrowserMediaStore = Reflux.createStore(
|
|
{
|
|
listenables: BrowserMediaActions
|
|
audio: null
|
|
loaded: false
|
|
loading: false
|
|
playing: false
|
|
paused: false
|
|
id: null
|
|
media_type: null
|
|
|
|
playbackState:(position, time) ->
|
|
state = {}
|
|
if @playing
|
|
state.playbackState = 'play_start'
|
|
else if @paused
|
|
state.playbackState = 'play_pause'
|
|
else
|
|
state.playbackState = 'play_stop'
|
|
|
|
state.playbackStateChanged = !@state? || @state.isPlaying != @playing || @state.paused != @paused
|
|
state.positionUpdateChanged = !@state? || @state.positionMs != position
|
|
state.currentTimeChanged = !@state? || @state.time != time
|
|
|
|
state
|
|
|
|
changed: () ->
|
|
position = @onGetPlayPosition()
|
|
time = context.JK.prettyPrintSeconds(parseInt(position / 1000))
|
|
playbackState = @playbackState(position, time)
|
|
|
|
# XXX: how to deal with duration? no mention in Howler API
|
|
|
|
target = {id: @id, isPlaying: @playing, loaded: @loaded, paused: @paused, positionMs: position, time: time, durationMs: @onGetPlayDuration()}
|
|
$.extend(true, target, playbackState)
|
|
@state = target
|
|
@trigger(@state)
|
|
|
|
onLoad: (id, urls, media_type) ->
|
|
if @audio
|
|
@audio.stop()
|
|
@audio.unload()
|
|
@loaded = false
|
|
@loading = false
|
|
@playing = false
|
|
@paused = false
|
|
|
|
@id = id
|
|
@media_type = media_type
|
|
@loading = true
|
|
@playing = false
|
|
@paused = false
|
|
|
|
console.log("URLS", urls)
|
|
@audio = new Howl({
|
|
src: urls,
|
|
autoplay: false,
|
|
loop: false,
|
|
volume: 1,
|
|
preload: true,
|
|
onend: @onAudioEnded,
|
|
onload: @onAudioLoaded,
|
|
onpause: @onAudioPause,
|
|
onplay: @onAudioPlay
|
|
})
|
|
|
|
@changed()
|
|
|
|
onPlay: () ->
|
|
if @audio
|
|
@playing = true
|
|
@paused = false
|
|
@audio.play()
|
|
|
|
try
|
|
if !@audio.recorded_play
|
|
rest.postUserEvent({name: @media_type + '_play'}) if @media_type?
|
|
context.stats.write('web.' + @media_type + '.play', {
|
|
value: 1,
|
|
user_id: context.JK.currentUserId,
|
|
user_name: context.JK.currentUserName
|
|
}) if @media_type
|
|
|
|
@audio.recorded_play = true
|
|
catch e
|
|
logger.warn("BrowserMediaStore: unable to post user event")
|
|
|
|
onPause: () ->
|
|
if @audio
|
|
@playing = false
|
|
@paused = true
|
|
@audio.pause()
|
|
|
|
onStop: () ->
|
|
if @audio
|
|
@playing = false
|
|
@paused = false
|
|
@audio.pause()
|
|
@audio.seek(0)
|
|
|
|
onSeek: (pos) ->
|
|
if @audio
|
|
console.log("seek time", pos)
|
|
@audio.seek(pos / 1000)
|
|
|
|
onGetPlayPosition: () ->
|
|
if @audio
|
|
try
|
|
position = @audio.seek()
|
|
if position == @audio
|
|
return 0
|
|
position * 1000
|
|
catch e
|
|
return 0
|
|
else
|
|
0
|
|
|
|
onGetPlayDuration: () ->
|
|
if @audio
|
|
# XXX : how to determine duration?
|
|
try
|
|
duration = @audio.duration()
|
|
return Math.round(duration) * 1000
|
|
catch e
|
|
return 0
|
|
else
|
|
0
|
|
|
|
onAudioEnded: () ->
|
|
logger.debug("onAudioEnded", this, arguments)
|
|
@playing = false
|
|
@changed()
|
|
|
|
onAudioLoaded: () ->
|
|
logger.debug("onAudioLoaded")
|
|
@loaded = true
|
|
@changed()
|
|
|
|
onAudioPause: () ->
|
|
logger.debug("onAudioPause")
|
|
|
|
@changed()
|
|
|
|
onAudioPlay: () ->
|
|
logger.debug("onAudioPlay")
|
|
@playing = true
|
|
@paused = false
|
|
@changed()
|
|
|
|
}
|
|
) |