更新的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