サンプル
preload="none"指定
console.logを使う
コード
var loader={
xhr: function(){
return new XMLHttpRequest();
},
download: function(url, key, tag){
var req=this.xhr();
req.open('GET', url, false);
req.overrideMimeType('text/plain; charset=x-user-defined');
req.send(null);
this.save(key, tag+base64encode(req.responseText));
},
save: function(key, value){
localStorage[key] = value;
},
get: function(key){
return localStorage[key];
},
has: function(key){
return Boolean( localStorage[key] );
},
clear: function(key){
if( key ){
delete localStorage[key];
}else{
localStorage.clear();
}
}
};
function saveAudio(key, elAudio){
var url, mime;
var elSources=elAudio.getElementsByTagName('source');
for( var i=0, elSource, t, s; elSource=elSources[i]; i++ ){
t=elSource.type;
s=elSource.src;
if( elAudio.canPlayType(t) ){
url=s;
mime=t;
break;
}
}
if( url ){
loader.download(url, key, 'data:'+mime+';base64,');
}
return url;
}
function playAudio(key){
//console.log( loader.get(key) );
var audio=new Audio(loader.get(key));
audio.play();
}
function playAudioOnPage(key){
//console.log( loader.get(key) );
var audio=document.getElementById('audio1');
audio.src=loader.get(key);
audio.load();
audio.play();
}
/**/
var key1='audio1';
onload= function(){
if( !loader.has(key1) ){
saveAudio(key1, document.getElementsByTagName('audio')[0]);
}
playAudio(key1);
};/*onload*/