Skip to content

Instantly share code, notes, and snippets.

@shekarsiri
Forked from asus4/angular-audio.coffee
Last active August 29, 2015 14:14
Show Gist options
  • Save shekarsiri/3aabbb72f56b3cb8f5b0 to your computer and use it in GitHub Desktop.
Save shekarsiri/3aabbb72f56b3cb8f5b0 to your computer and use it in GitHub Desktop.
# This is Simple Audio Plugin for AngularJS
# service
class AngularAudio
constructor:() ->
@cache = {}
play:(id) ->
audio = null
if (@cache[id]) # check cache
audio = @cache[id]
else # load from DOM
audio = document.querySelector(id)
@cache[id] = audio
if audio
@playSE(audio)
playSE:(audio) ->
audio.load()
audio.play()
#================
# Assign modules
#================
_module = angular.module('ngAudio', [])
_module.directive('ngAudio', ($compile, ngAudio) ->
return {
restrict: 'AE'
controller: ($scope, $attrs, $element) ->
$element.on('click', (e) ->
ngAudio.play($attrs.ngAudio)
)
}
)
_module.service('ngAudio', AngularAudio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment