var LibCanvasAudio = function ( file ) {
this . audio = new Audio ;
this . audio . src = file ;
};
LibCanvasAudio . prototype = {};
var LibCanvasAudio = function ( file ) {
this . audio = new Audio ;
this . src ( file );
};
LibCanvasAudio . prototype = {
src : function ( file ) {
var codec = this . getSupport ();
if (! codec ) throw 'AudioNotSupported' ;
this . audio . src = file . replace (/\*/ g , this . getSupport ());
this . audio . load ();
return this ;
},
getSupport : function () {
return ! this . audio . canPlayType ? false :
this . audio . canPlayType ( 'audio/ogg;' ) ? 'ogg' :
this . audio . canPlayType ( 'audio/mpeg;' ) ? 'mp3' : false ;
}
}
LibCanvasAudio . prototype = {
// ...
cloneAudio : function () {
audioClone = this . audio . cloneNode ( true );
audioClone . load ();
return audioClone ;
},
gatling : function ( count ) {
this . barrels = [];
this . gatIndex = 0 ;
while ( count --) {
this . barrels . push ( this . cloneAudio ());
}
return this ;
},
getNext : function () {
var elem = this . barrels [ this . gatIndex ];
++ this . gatIndex >= this . barrels . length && ( this . gatIndex = 0 );
return elem ;
},
playNext : function () {
var elem = this . getNext ();
elem . pause ();
elem . currentTime = 0 ;
elem . play ();
return this ;
}
};
var shotSound = new LibCanvasAudio ( 'explosion.*' ). gatling ( 6 );
window . addEventListener ( 'keydown' , function ( e ) {
( e . keyCode == keys . SPACE ) && shotSound . playNext ();
}, false );
// var audioOrig = document.createElement('audio'); // :
var audioOrig = new Audio ();
audioOrig . src = 'shot.ogg' ;
audioOrig . controls = 'controls' ;
var audioClone = audioOrig . cloneNode ( true );
function appendToBody ( node ) {
document . getElementsByTagName ( 'body' )[ 0 ]. appendChild ( node );
}
audioOrig . play (); //
audioClone . play (); //
appendToBody ( audioOrig ); //
appendToBody ( audioClone ); //
LibCanvasAudio . prototype = {
// ..
cloneAudio : function () {
if ( window . opera ) { // Reported Opera bug DSK-309302
var audioClone = new Audio ;
audioClone . src = this . audio . src ;
} else {
audioClone = this . audio . cloneNode ( true );
}
audioClone . load ();
return audioClone ;
},
// ..
};
LibCanvasAudio . prototype = {
// ..
playNext : function () {
var elem = this . getNext ();
elem . pause ();
elem . currentTime = 0.025 ;
elem . play ();
return this ;
}
};
new Audio()
does not work, but it is very easy to solve by replacing it with document.createElement('audio');
Source: https://habr.com/ru/post/101145/
All Articles