Permalink
Browse files

experiments with audio and loader

  • Loading branch information...
1 parent 3f50545 commit 4ef08d9ec27d685e526afd3629b42b99eee9e1b8 @BlackScorp committed Mar 4, 2012
Showing with 449 additions and 9 deletions.
  1. +87 −0 assets/js/audio.js
  2. +5 −4 assets/js/crafty.js
  3. +305 −0 assets/js/loader.js
  4. +41 −5 assets/js/sounds.js
  5. +6 −0 assets/js/sprites.js
  6. +5 −0 index.html
View
@@ -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;
+ }
+ }
+});
View
@@ -6165,7 +6165,7 @@ Crafty.c("particles", {
canplay = audio.canPlayType(this.type[ext]);
//if browser can play this type, use it
- if (canplay !== "" && canplay !== "no"&& canplay !== "maybe") {
+ if (canplay !== "" && canplay !== "no") {
url = source;
break;
}
@@ -6199,7 +6199,7 @@ Crafty.c("particles", {
canplay = audio.canPlayType(this.type[ext]);
//if browser can play this type, use it
- if (canplay !== "" && canplay !== "no" && canplay !== "maybe") {
+ if (canplay !== "" && canplay !== "no") {
url = source;
break;
}
@@ -7219,10 +7219,11 @@ Crafty.c("Text", {
if (Crafty.support.audio && (ext === "mp3" || ext === "wav" || ext === "ogg" || ext === "mp4")) {
obj = new Audio(current);
- event = 'loadstart';
+ event = 'canplay';
canplay = obj.canPlayType(Crafty.audio.type[ext]);
- if(canplay !== "" && canplay !== "no" && canplay !=="maybe"){
+ if(canplay !== "" && canplay !== "no"){
+ obj.load();
this.assets[current] = obj;
}
Oops, something went wrong.

0 comments on commit 4ef08d9

Please sign in to comment.