* Player audio files (.mp3, .wav and .ogg)
*
* @namespace LUNGO.Sugar
* @class Sound
*
*/
LUNGO.Sugar.Sound = (function(lng, undefined) {
var _background = document.createElement('audio');
var _sound = document.createElement('audio');
/**
* Plays music in background with automatic rewind.
*
* @method background
*
* @param {string} Source of sound file
*/
var background = function(source) {
if (source) {
_setSourceAndPlay(_background, source);
_background.addEventListener('ended', function(){
this.currentTime = 0;
}, false);
}
else {
_background.pause();
}
};
/**
* Play a given sound
*
* @method play
*
* @param {string} Source of sound file
*/
var play = function(source) {
_setSourceAndPlay(_sound, source);
};
_setSourceAndPlay = function(container, source) {
container.setAttribute('src', source);
};
return {
background: background,
play: play
}
})(LUNGO);
The javascript code is:
Game.Sound = (function(lng, undefined) {
var RESOURCES = {
BACKGROUND: 'assets/audio/background.mp3',
BUTTON: '',
WIN: ''
};
var _active = true;
toggle = function() {
if (_active) {
off();
} else {
on();
}
};
on = function() {
lng.Sugar.Sound.background(RESOURCES.BACKGROUND);
_active = true;
_visibility();
};
off = function() {
lng.Sugar.Sound.background(null);
_active = false;
_visibility();
};
button = function() {
_play(RESOURCES.BUTTON)
};
win = function() {
_play(RESOURCES.WIN);
};
_play = function(resource) {
if (_active) {
}
};
_visibility = function() {
var el = lng.dom('a[data-control=sound]');
if (_active) {
el.removeClass('red').children('.icon').removeClass('bell-off').addClass('bell-on');
} else {
el.addClass('red').children('.icon').removeClass('bell-on').addClass('bell-off');
}
}
return {
on: on,
off: off,
toggle: toggle,
button: button,
win: win
}
})(LUNGO);