Created
October 25, 2016 11:06
-
-
Save AndrePessoa/2c189bcea3d27d8d443f9db72082bc82 to your computer and use it in GitHub Desktop.
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
var som = { | |
$el: $('#botaoMute'), | |
muted: false, | |
playlist: {}, | |
list: [ | |
{ id:'acerto', url:'./jogos/audio/acerto.mp3', loop: false, volume:1 }, | |
{ id:'erro', url:'./jogos/audio/erro.mp3', loop: false, volume:1 }, | |
{ id:'trilha1', url:'./jogos/audio/trilha1.mp3', loop: true, volume:0.2 }, | |
{ id:'trilha2', url:'./jogos/audio/trilha2.mp3', loop: true, volume:0.2 }, | |
], | |
init: function(){ | |
this.addEvents(); | |
}, | |
addEvents: function(){ | |
var self = this; | |
this.$el.on('click', function(){ | |
self.toggle(); | |
$(this).toggleClass('muted', self.muted ); | |
}); | |
}, | |
preload: function( cb ){ | |
var count = 0; | |
var self = this; | |
function tryInit(){ | |
if( count >= self.list.length ){ | |
cb(); | |
} | |
}; | |
for (var i = this.list.length - 1; i >= 0; i--) { | |
var sound = this.list[i]; | |
this.add( | |
sound.id, | |
sound.url, | |
sound.loop, | |
sound.volume, | |
function(){ count++; tryInit(); } | |
); | |
} | |
}, | |
add: function( id, url, loop, volume, cb ){ | |
if( this.playlist[id] ) return false; | |
var som = new Audio( url ); | |
som.loop = loop || false; | |
som.volume = volume !== undefined ? volume : 1 ; | |
som._volume = volume !== undefined ? volume : 1 ; | |
var self = this; | |
$(som).on("canplaythrough", function() { | |
console.log('som carregado'); | |
self.playlist[id] = som; | |
if( cb ) cb(); | |
}); | |
return this; | |
}, | |
start: function( id ){ | |
if( this.playlist[id] == undefined ) return false; | |
this.stop(id); | |
this.playlist[id].play(); | |
}, | |
pause: function( id ){ | |
if( id == undefined ){ | |
for( var som in this.playlist ){ | |
this.playlist[ som ].pause(); | |
} | |
}else{ | |
this.playlist[id].pause(); | |
} | |
}, | |
stop: function( id ){ | |
if( id == undefined ){ | |
for( var som in this.playlist ){ | |
this.playlist[ som ].pause(); | |
this.playlist[ som ].currentTime = 0; | |
} | |
}else{ | |
this.playlist[id].pause(); | |
this.playlist[id].currentTime = 0; | |
} | |
}, | |
toggle: function(){ | |
this.muted = !this.muted; | |
for( var som in this.playlist ){ | |
this.playlist[ som ].volume = ( this.muted )? 0 : this.playlist[ som ]._volume ; | |
} | |
} | |
}; | |
this.som = som; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment