Permalink
Browse files

Automatically play the next track when one ends.

  • Loading branch information...
1 parent c526c10 commit 277ef281dbdc40b02fb897cd8dc31f5d5e3bc27b @jimmycuadra committed Nov 3, 2012
Showing with 9 additions and 1 deletion.
  1. +5 −0 source/javascripts/models/music_player.js.coffee
  2. +4 −1 source/javascripts/models/sound.js.coffee
View
5 source/javascripts/models/music_player.js.coffee
@@ -5,10 +5,15 @@ class ph.MusicPlayer extends Backbone.Model
initialize: ->
@collection.on("change:state", @stateChanged, this)
+ @collection.on("ended", @playNextTrack, this)
stateChanged: (model, state, attributes) ->
@set(state: state)
+ playNextTrack: ->
+ @nextTrack()
+ @play()
+
currentTrack: ->
@collection.at(@get("currentIndex"))
View
5 source/javascripts/models/sound.js.coffee
@@ -5,10 +5,13 @@ class ph.Sound extends Backbone.Model
initialize: ->
@_audio = document.getElementById(@get("name"))
@_audio.volume = 0.3 if @get("music")
- @_audio.addEventListener("ended", @stateChanged)
+ @_audio.addEventListener("ended", @ended)
@_audio.addEventListener("pause", @stateChanged)
@_audio.addEventListener("play", @stateChanged)
+ ended: (event) =>
+ @trigger("ended")
+
stateChanged: (event) =>
if @_audio.paused
@set(state: "paused")

0 comments on commit 277ef28

Please sign in to comment.