Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackScorp committed Mar 7, 2012
1 parent 89715a4 commit aeb6f48
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 90 deletions.
90 changes: 55 additions & 35 deletions assets/js/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,96 @@ Crafty.extend({
'mp4': 'audio/mp4'
},
volume:1, //Global Volume
muted:false,
add:function(id,url){
if (!Crafty.support.audio) return;

var audio,source,ext,path,canplay;
var audio,source,ext,path;
if(arguments.length === 1 && typeof id === "object"){
for(var i in id){
audio = document.createElement('audio');
audio.id = i;
//old attribute
audio.autobuffer = true;
audio.preload = "auto";
audio.volume = Crafty.audio.volume;

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

if (!Crafty.assets[path]) Crafty.assets[path] = audio;
}

source = document.createElement('source');
source.src = path;
source.type=this.srcType[ext];
audio.appendChild(source);
}
this.sounds[i] = {
obj:audio,
played:0
}
audio.load();


}



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


audio = document.createElement('audio');
audio.id = id;
audio.preload = "auto";
audio.volume = Crafty.audio.volume;
if(typeof url === "string"){
audio.src = url;
if (!Crafty.assets[url]) Crafty.assets[url] = audio;
}

if(typeof url === "object"){
for(src in url){
path = url[src];
ext = path.substr(path.lastIndexOf('.') + 1).toLowerCase();
source = document.createElement('source');
source.src = path;
source.type=this.srcType[ext];
audio.appendChild(source);
if (!Crafty.assets[path]) Crafty.assets[path] = audio;

}
}
this.sounds[id] = {
obj:audio,
played:0
}
audio.load();
}


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

var s = this.sounds[id];

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

if(s.obj.currentTime) s.obj.currentTime = 0;
s.obj.play();
s.played ++;

function r(){
s.obj.onended = function(){
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);
};
},
mute:function(){
var s;
if(!this.muted){
for(var i in this.sounds){
s = this.sounds[i];
s.obj.pause();
}
this.muted = true;
}else{
for(var i in this.sounds){
s = this.sounds[i];
if(s.obj.currentTime && s.obj.currentTime > 0)
this.sounds[i].obj.play();
}
this.muted = false;
}
return s;

}
}
});
4 changes: 3 additions & 1 deletion assets/js/crafty.js
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,9 @@
}
return id;
}

if(window.opera){
console = {log:opera.postError}
}
/**
* Clone an Object
*/
Expand Down
1 change: 1 addition & 0 deletions assets/js/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Crafty.scene("Loading",function(){
//uh oh, error loading
console.log("Error on loading: ");
console.log(e.obj);

}
);

Expand Down
109 changes: 55 additions & 54 deletions assets/js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,74 +51,75 @@ Crafty.extend({
*/
load: function (data, oncomplete, onprogress, onerror) {

var i = 0, l = data.length, current, obj, total = l, j = 0, ext = "",event,canplay;

var i = 0, l = data.length, current, obj, total = l, j = 0, ext = "" ,audio,canplay;

//Progress function
function pro(){

++j;
//if progress callback, give information of assets loaded, total and percent
if (onprogress)
onprogress.call(this,{
loaded: j,
total: total,
percent: (j / total * 100),
obj:this
});

if(j === total && oncomplete) oncomplete();
};
//Error function
function err(){

if (onerror)
onerror.call(this,{
loaded: j,
total: total,
percent: (j / total * 100),
obj:this
});

j++;
if(j === total && oncomplete) oncomplete();
};

for (; i < l; ++i) {
current = data[i];
event = '';
ext = current.substr(current.lastIndexOf('.') + 1).toLowerCase();
if(this.assets[current]){
obj = this.assets[current];

}else{
obj =null;
}


if (Crafty.support.audio && (ext === "mp3" || ext === "wav" || ext === "ogg" || ext === "mp4")) {
event = 'loadedmetadata';

//Chrome has problems with mp3 canplaystate is maybe
if (navigator.userAgent.indexOf('Chrome') != -1 && ext === "mp3") j++;


} else if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png") {
event = 'load';
if(!obj){
obj = document.createElement('audio');
obj.preload = "auto";
obj.volume = Crafty.audio.volume;
canplay = obj.canPlayType(Crafty.audio.types[ext]);
if(canplay !== "" && canplay !== "no"){
obj.src =current;
}
obj.load();
if (!Crafty.assets[current]) Crafty.assets[current] = obj;
}
//Chrome does not support autoload
if (navigator.userAgent.indexOf('Chrome') != -1) pro();
obj.onloadeddata=pro;

} else if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png") {
if(!obj){
obj = new Image();
obj.src = current;
if (!Crafty.assets[current]) Crafty.assets[current] = obj;
}
obj.onload = pro;
} else {
total--;
continue; //skip if not applicable
}

//Progress function
function pro(){

++j;
//if progress callback, give information of assets loaded, total and percent
if (onprogress)
onprogress({
loaded: j,
total: total,
percent: (j / total * 100),
obj:this
});

if(j === total && oncomplete) oncomplete();
};
//Error function
function err(e){

if (onerror)
onerror({
loaded: j,
total: total,
percent: (j / total * 100),
obj:this
});

j++;
if(j === total && oncomplete) oncomplete();
};

if (obj.attachEvent) { //IE

obj.attachEvent('on' + event, pro);
obj.attachEvent('onerror', err);
} else { //Everyone else
obj.addEventListener(event, pro, false);
obj.addEventListener('error', err, false);
}


obj.onerror = err;
}


Expand Down
7 changes: 7 additions & 0 deletions assets/js/sounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ Crafty.audio.add({
]
});

Crafty.audio.add("space2", game_path + "media/music/through-space.mp3");

Crafty.audio.add("space3",[
game_path + "media/music/through-space.mp3",
game_path + "media/music/through-space.ogg"
]);
Crafty.audio.add("space4");

0 comments on commit aeb6f48

Please sign in to comment.