更新的iOS 6:
這一切都是在固定的iOS 6,您可以緩存的音頻文件,並通過JavaScript播放而無需用戶輸入。
shaun5 11月7日在'12 0:58
即使它應該工作,沒有規範(W3C,蘋果),它說,它不應該,我測試了一些場景似乎iOS上的Safari只是拒絕緩存聲音文件。
Safari瀏覽器加載音頻文件(但不是的index.html)每次我重新加載頁面的時間,所以緩存顯然沒有工作的文件大小的正確regardlessly。
經過一番研究:它看起來像它不可能緩存的音頻文件。所以,你可以在文件蘋果bugtracker的錯誤。
這裏是我的代碼,以證明我的結果:
的index.html:
<!DOCTYPE HTML>
<html lang="en-US" manifest="audio.appcache">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>Cached audio</title>
</head>
<body>
<h1>Cached audio</h1>
<audio controls autobuffer>
<source src="sample.mp3" type="audio/mpeg" />
</audio>
</body>
</html>
audio.appcache:
CACHE MANIFEST
# 2012-02-23:v1
# explicitly cached
CACHE:
index.html
sample.mp3
的.htaccess:
AddType text/cache-manifest .appcache
AddType audio/mpeg .mp3
編輯
我也使用數據URI嘗試,但,Safari瀏覽器一直拒絕音頻數據。因此,我認爲這是不可能的緩存音頻...
<?php
function data_uri($file) {
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:audio/mpeg;base64,'.$base64);
}
?>
...
<audio controls autobuffer>
<source src="<?php echo data_uri('sample.mp3'); ?>" type="audio/mpeg" />
</audio>
...
EDIT 2
好主意,但它看起來像它不工作... Safari瀏覽器爲OS X - >確定;適用於iOS的Safari - >仍然是我們之前遇到的相同問題。
<?php
function base_64_string($file) {
$contents = file_get_contents($file);
return base64_encode($contents);
}
?>
...
<audio controls autobuffer>
<source id="sound-src" src="sample.mp3" type="audio/mpeg" />
</audio>
...
<script>
(function(){
var sound;
if (localStorage.getItem('cachedSound')) {
sound = localStorage.getItem('cachedSound');
}
else {
sound = "<?php echo base_64_string('sample.mp3'); ?>";
localStorage.setItem('cachedSound',sound);
}
document.getElementById("sound-src").src='data:audio/mpeg;base64,' + sound;
})();
</script>
來源
2012-02-23 09:39:04
dom
您是否檢查清單的內容是否實際下載?也許這只是一個錯字... – andreapier 2012-02-19 21:26:41
不知道更多關於你在做什麼,真的很難說它是否是你的代碼。 – livingtech 2012-02-21 16:30:47
添加示例代碼。 – shaun5 2012-02-22 05:14:21