2012-02-13 53 views
7

當我加我嘟嘟23.mp3文件緩存清單,聲音效果不再適用於或離線。這是一個錯誤,還是我做錯了什麼?可以緩存使用清單或Web存儲在iOS的web應用程序的聲音文件?

音頻是一個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'); 
} 

通過下列方式訪問:

$("#mybutton,#anotherbutton").each(function() { 
    $(this).bind("touchstart",function(e){ 
      playBEEP(); 
    }); 
}); 

<html manifest='index.manifest'>使聲音停止上市嗶23.mp3時工作......

UPDATE:Web Storage被用來代替緩存清單來存儲音頻?

+0

您是否檢查清單的內容是否實際下載?也許這只是一個錯字... – andreapier 2012-02-19 21:26:41

+0

不知道更多關於你在做什麼,真的很難說它是否是你的代碼。 – livingtech 2012-02-21 16:30:47

+0

添加示例代碼。 – shaun5 2012-02-22 05:14:21

回答

7

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

您的回答讓我想到了一個可能的解決方案,請參閱我的更新問題。 – shaun5 2012-02-23 11:00:34

+0

@ shaun5看看新的編輯... – dom 2012-02-23 11:37:59

+0

我想你必須等待用戶在移動Safari下載文件之前播放文件,所以我認爲你將不得不等待將它保存到本地存儲。 – shaun5 2012-02-23 12:00:24

0

如果你還沒有,你必須確保URL路徑是絕對或相對.manifest的,而不是實際的網頁。

您嘗試緩存的實際組件的大小也有4MB的限制。

總緩存限制爲5MB。

我也很肯定你不能緩存音頻和視頻文件。

+0

所有三個文件(html,mp3,manifest)都在同一個目錄中。總的和單個文件大小都在限制之下。你有任何經驗或文件來支持你'非常確定'嗎? – shaun5 2012-02-23 12:35:04

+0

@ shaun5我在蘋果開發者網站上做了大量的閱讀,沒有什麼可說的。我也嘗試過自己做這件事,除了視頻以外,所有內容都被緩存了。雖然我沒有音頻文件。通過消除的過程,我不得不假設你不能。 – 2012-02-23 15:02:12

-3

Apple不支持通過Safari瀏覽器播放音頻,這是爲了防止用戶使用在線域名存儲音樂而無需支付費用並使用iPod/iPhone播放音樂。這是試圖讓你爲音樂付費的蘋果方式。對不起大家。

+1

這個答案似乎直接矛盾的問題和其他答案。 – 2012-08-30 10:52:19

+0

這是不正確的。 – 2012-10-03 00:44:16

相關問題