您可以使用清单或Web存储在iOS Web应用程序中缓存声音文件吗?

sha*_*un5 7 iphone web-applications ipad ios ios5

当我将beep-23.mp3文件添加到缓存清单时,声音效果不再适用于或脱机.这是一个错误,还是我做错了什么?

音频在html文件中,如下所示:

function playBEEP() { if (navigator.platform == "iPad" || navigator.platform == "iPhone" || navigator.platform == "iPod") { Beep.play(); } }
if (navigator.platform == "iPad" || navigator.platform == "iPhone" || navigator.platform == "iPod") {
    var Beep = document.createElement('audio');
    Beep.setAttribute('src', 'beep-23.mp3');
}
Run Code Online (Sandbox Code Playgroud)

访问过:

$("#mybutton,#anotherbutton").each(function() {
    $(this).bind("touchstart",function(e){ 
            playBEEP();
    });
});
Run Code Online (Sandbox Code Playgroud)

<html manifest='index.manifest'> 当beep-23.mp3列出时,音频停止工作...

更新:是否可以使用Web存储而不是缓存清单来存储音频?

dom*_*dom 7

适用于iOS 6的更新:

这在iOS 6中已得到修复.您可以缓存音频文件并通过javascript播放而无需用户输入.
shaun5年 11月7日在0:58


即使它应该工作,没有规格(W3C,Apple),它说它不应该,我测试了一些场景,似乎iOS上的Safari只是拒绝缓存声音文件.

Safari每次重新加载页面时都会加载音频文件(但不是index.html),因此无论文件大小如何,缓存显然都无法正常工作.

经过一番研究:看起来无法缓存音频文件.所以你可以在Apples bugtracker上提交一个bug.

这是我的代码来证明我的结果:

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>
Run Code Online (Sandbox Code Playgroud)

audio.appcache:

CACHE MANIFEST
# 2012-02-23:v1

# explicitly cached
CACHE:
index.html
sample.mp3
Run Code Online (Sandbox Code Playgroud)

的.htaccess:

AddType text/cache-manifest .appcache
AddType audio/mpeg .mp3
Run Code Online (Sandbox Code Playgroud)

编辑

我也尝试使用数据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>
...
Run Code Online (Sandbox Code Playgroud)

编辑2

好主意,但看起来它不起作用... OS X for OS X - > ok; 适用于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>
Run Code Online (Sandbox Code Playgroud)