Permalink
Browse files

Audio refactoring

  • Loading branch information...
1 parent 880fc6d commit 7110c83a47e47f975d41daa10cf8b0e1ddc4dd6f @sgrove sgrove committed Apr 13, 2012
Showing with 55 additions and 14 deletions.
  1. +55 −14 app/assets/javascripts/backbone/plugins/music_player.js.coffee
@@ -5,11 +5,14 @@ class Kandan.Plugins.MusicPlayer
@widgetTitle: "Player"
@playRegex: /^/play .+/
@stopRegex: /^/stop/
+ @resumeRegex: /^/resume/
@localSongData: false
- @playTemplate: _.template('<strong><a class="audio-play">playing</a> <a target="_blank" href="<%- url %>"><%- url %></a></strong>')
- @songTemplate: _.template('<li><%= song.split("/").pop() %></li>')
+ @playTemplate: _.template('<strong><a class="audio-play">playing</a> <a target="_blank" href="<%- url %>"><%- url %></a></strong>')
+ @stopTemplate: _.template('<strong><a class="audio-play">stopping</a> the music.')
+ @resumeTemplate: _.template('<strong><a class="audio-play">resuming</a> the music.')
+ @songTemplate: _.template('<li><%= song.split("/").pop() %></li>')
@setError: (errorMessage)->
@@ -45,25 +48,50 @@ class Kandan.Plugins.MusicPlayer
@init: (pluginId)->
@pluginId = pluginId
Kandan.Data.Channels.registerCallback("change", $.proxy(@onChannelChange, this))
- @registerModifier()
- @registerWidget()
+ @registerPlayModifier()
+ @registerStopModifier()
+ @registerResumeModifier()
+ # Disabled for now
+ #@registerWidget()
@registerWidget: ()->
Kandan.Widgets.register @pluginNamespace
- @registerModifier: ()->
+ @registerPlayModifier: ()->
Kandan.Modifiers.register @playRegex, (message, state) =>
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
if true and Kandan.Data.Channels.activeChannelId()? # and state == Kandan.Helpers.Activities.ACTIVE_STATE commented out because state == undefined for some reason
- @playUrl(message.channel_id, Kandan.Helpers.Utils.unescape(url))
+ rawInput = Kandan.Helpers.Utils.unescape(url)
+ soundUrl = null
+ soundUrl = @localSounds(rawInput)
+ soundUrl ?= rawInput
+
+ @playUrl(message.channel_id, soundUrl)
else
console.log "Not playing stale song"
message.content = @playTemplate({url: url})
return Kandan.Helpers.Activities.buildFromBaseTemplate message
+ @registerStopModifier: ()->
+ Kandan.Modifiers.register @stopRegex, (message, state) =>
+ url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
+ if true and Kandan.Data.Channels.activeChannelId()?
+ @stopSound(message.channel_id)
+
+ message.content = @stopTemplate()
+ return Kandan.Helpers.Activities.buildFromBaseTemplate message
+
+ @registerResumeModifier: ()->
+ Kandan.Modifiers.register @resumeRegex, (message, state) =>
+ if true and Kandan.Data.Channels.activeChannelId()?
+ @play(message.channel_id)
+
+ message.content = @resumeTemplate()
+ return Kandan.Helpers.Activities.buildFromBaseTemplate message
+
# TODO display error about song not being added by creating an activity locally
@storeSong: (url)->
@@ -81,17 +109,18 @@ class Kandan.Plugins.MusicPlayer
@getSongs: (callbacks)->
Kandan.Store.get @pluginId, callbacks
- @nicknameToUrl: (url) ->
+ @localFileUrl: (fileName) ->
+ "http://#{ window.location.hostname }:#{ window.location.port }/sounds/#{ fileName }"
+
+ @localSounds: (name) ->
sounds = {
- 'claps' : 'golfclap.mp3',
- 'cheers' : 'cheers.mp3',
- 'ding' : 'ding.wav',
- 'gong' : 'gong.mp3'
+ 'claps' : @localFileUrl('golfclap.mp3')
+ 'cheers' : @localFileUrl('cheers.mp3')
+ 'ding' : @localFileUrl('ding.wav')
+ 'gong' : @localFileUrl('gong.mp3')
}
- fullUrl = null
- fullUrl = sounds[url]
- fullUrl ||= url
+ sounds[name]
@audioChannels: ->
Kandan.Helpers.Audio.audioChannels()
@@ -119,8 +148,14 @@ class Kandan.Plugins.MusicPlayer
@playUrl: (channelId, url) ->
@setAudioUrl(channelId, url)
+ @play(channelId)
+
+ @play: (channelId) ->
@audioChannel(channelId).play()
+ @stopSound: (channelId) ->
+ @audioChannel(channelId).pause()
+
@currentChannel: () ->
Kandan.Data.Channels.activeChannelId()
@@ -135,4 +170,10 @@ class Kandan.Plugins.MusicPlayer
if @audioChannel(channelId)?
@unmute(channelId)
+ @playAudioNotice: ->
+ url = @localFileUrl('ding.wav')
+ player = $('.audio_private')[0]
+ player.setAttribute('src', url)
+ player.play()
+
# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"

0 comments on commit 7110c83

Please sign in to comment.