Skip to content

Commit

Permalink
experiments with audio and loader
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackScorp committed Mar 4, 2012
1 parent 3f50545 commit 4ef08d9
Show file tree
Hide file tree
Showing 6 changed files with 449 additions and 9 deletions.
87 changes: 87 additions & 0 deletions assets/js/audio.js
@@ -0,0 +1,87 @@
Crafty.extend({
audio:{
sounds:{},
type: {
'mp3': 'audio/mpeg;',
'ogg': 'audio/ogg; codecs="vorbis"',
'wav': 'audio/wav; codecs="1"',
'mp4': 'audio/mp4; codecs="mp4a.40.2"'
},
srcType: {
'mp3': 'audio/mpeg',
'ogg': 'audio/ogg',
'wav': 'audio/wav',
'mp4': 'audio/mp4'
},

add:function(id,url){
if (!Crafty.support.audio) return this;
var audio,source,ext,url,canplay;
if(arguments.length === 1 && typeof id === "object"){
for(var i in id){
audio = document.createElement('audio');
audio.id = i;
audio.autobuffer = true;
audio.preload = "auto";
audio.volume = 1;


for(var src in id[i]){
url = id[i][src];
ext = url.substr(url.lastIndexOf('.') + 1).toLowerCase();
canplay = audio.canPlayType(this.type[ext]);
if(canplay === "probably"){
source = document.createElement('source');
source.src = url;
source.type=this.srcType[ext];

audio.appendChild(source);
if (!Crafty.assets[url]) Crafty.assets[url] = audio;


}
}
this.sounds[i] = {
obj:audio,
played:0
}
}


}
if(typeof id === "string"){


}


},
play:function(id,repeat,volume){
if(repeat == 0 || !Crafty.support.audio || !this.sounds[id]) return;

var s = this.sounds[id];

s.obj.volume = 1 || volume;
if(s.obj.currentTime) s.obj.currentTime = 0;

// s.obj.mozCurrentSampleOffset = -10;
s.obj.play();
s.played ++;

function r(){
if(s.played < repeat || repeat == -1){
if(this.currentTime) this.currentTime = 0;
this.play();
s.played ++;
}

}
if (s.attachEvent) { //IE
s.obj.attachEvent('onended', r);
} else { //Everyone else
s.obj.addEventListener('ended', r, false);
}
return s;
}
}
});
9 changes: 5 additions & 4 deletions assets/js/crafty.js
Expand Up @@ -6165,7 +6165,7 @@ Crafty.c("particles", {
canplay = audio.canPlayType(this.type[ext]); canplay = audio.canPlayType(this.type[ext]);


//if browser can play this type, use it //if browser can play this type, use it
if (canplay !== "" && canplay !== "no"&& canplay !== "maybe") { if (canplay !== "" && canplay !== "no") {
url = source; url = source;
break; break;
} }
Expand Down Expand Up @@ -6199,7 +6199,7 @@ Crafty.c("particles", {
canplay = audio.canPlayType(this.type[ext]); canplay = audio.canPlayType(this.type[ext]);


//if browser can play this type, use it //if browser can play this type, use it
if (canplay !== "" && canplay !== "no" && canplay !== "maybe") { if (canplay !== "" && canplay !== "no") {
url = source; url = source;
break; break;
} }
Expand Down Expand Up @@ -7219,10 +7219,11 @@ Crafty.c("Text", {
if (Crafty.support.audio && (ext === "mp3" || ext === "wav" || ext === "ogg" || ext === "mp4")) { if (Crafty.support.audio && (ext === "mp3" || ext === "wav" || ext === "ogg" || ext === "mp4")) {
obj = new Audio(current); obj = new Audio(current);


event = 'loadstart'; event = 'canplay';
canplay = obj.canPlayType(Crafty.audio.type[ext]); canplay = obj.canPlayType(Crafty.audio.type[ext]);


if(canplay !== "" && canplay !== "no" && canplay !=="maybe"){ if(canplay !== "" && canplay !== "no"){
obj.load();
this.assets[current] = obj; this.assets[current] = obj;
} }


Expand Down

0 comments on commit 4ef08d9

Please sign in to comment.