JSFiddle - React, Tailwind, and code Playground
JavaScript
function WebAudio(aAudioSource) {
this.mAudioPlayer = new Audio();
this.mAudioData = '';
this.mExtension = '';
this.mAudioSrc = '';
this.miTime = .1;
if (this.mAudioPlayer.canPlayType("audio/mp4")) {
this.mExtension = '.m4a'; //TODO: we want this to take precedence
} else if (this.mAudioPlayer.canPlayType("audio/ogg")) {
this.mExtension = '.ogg';
} else if (this.mAudioPlayer.canPlayType("audio/mpeg")) {
this.mExtension = '.mp3';
}
this.CreateAudioData = function() {
//linking directly to the audio
if( window.userId != undefined ){
this.mAudioSrc = aAudioSource + this.mExtension + "?authorization=" + window.userId;
}else if( window.cak != undefined ){
this.mAudioSrc = aAudioSource + this.mExtension + "?cak=" + window.cak;
}
this.mAudioPlayer = new Audio();
this.mAudioPlayer.retries = 5;
this.mAudioPlayer.tried = 0;
this.mAudioPlayer.loaded = false;
this.mAudioPlayer.time = 0;
this.mAudioPlayer.autoPlay = false;
$(this.mAudioPlayer).bind({
error: function(){
if( this.tried != this.retries ){
this.src = this.src;
this.tried++;
var reason = "Error";
switch( this.error.code ){
case 1:
reason = "Aborted";
break;
case 2:
reason = "Decode";
break;
case 3:
reason = "Network";
break;
case 4:
reason = "Not_Supported";
break;
}
HandleWarning("Audio was restarted from error state because of " + reason);
}else{
HandleError("Audio failed to load");
}
},
canplay: function(){
this.loaded = true;
if( this.autoPlay ){
this.play();
}
}
});
this.mAudioPlayer.src = this.mAudioSrc;
//downloading audio for use in data:uri
/*
$.ajax({
url: aAudioSource + this.mExtension + '.txt',
type: 'GET',
context: this,
async: false,
beforeSend: function(xhr) {xhr.setRequestHeader('Authorization',...