Last active
August 14, 2018 13:45
-
-
Save oronbz/11015060 to your computer and use it in GitHub Desktop.
This is a soundPlayer for AngularJS using the soundManager2 API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// download soundManager2 and refer it in your scripts http://www.schillmania.com/projects/soundmanager2/doc/download/#latest | |
// also add the swf directory to your project | |
// your module declaration | |
var app = angular.module('myApp', []); | |
// attach the soundManager global for AngularJS dependency injection | |
app.value('soundManager', soundManager); | |
// usage | |
app.controller('homeController', [ | |
'$scope','soundPlayer', function ($scope, soundPlayer) { | |
$scope.playSound = function () { | |
//Browser support for HTML5 Audio varies, and format support | |
//(eg. MP3, MP4/AAC, OGG, WAV) can vary by browser/platform. | |
soundPlayer.play('REPLACE_WITH_YOUR_SOUND_URL'); | |
} | |
} | |
]); | |
// the soundPlayer service | |
app.factory('soundPlayer', [ | |
'soundManager', function (soundManager) { | |
return { | |
// the method play gets a relative/absolute sound file url to play | |
play: function (url) { | |
soundManager.setup({ | |
// set debugMode to 'false' for less verbose | |
debugMode: true, | |
url: 'REPLACE_WITH_YOUR_SWF_PATH', | |
onready: function () { | |
var mySound = soundManager.createSound({ | |
url: url | |
}); | |
mySound.play(); | |
}, | |
// some error handling | |
ontimeout: function () { | |
// Hrmm, SM2 could not start. Missing SWF? Flash blocked? Show an error, etc.? | |
} | |
}); | |
} | |
} | |
} | |
]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment