Skip to content

Instantly share code, notes, and snippets.

@oronbz
Last active August 14, 2018 13:45
Show Gist options
  • Save oronbz/11015060 to your computer and use it in GitHub Desktop.
Save oronbz/11015060 to your computer and use it in GitHub Desktop.
This is a soundPlayer for AngularJS using the soundManager2 API.
// 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