2014-01-07 57 views
0

如果內容更改但名稱仍然相同,如何在HTML 5中重新讀取音頻文件(.WAV)?我已經實現了這一點:HTML 5音頻重讀

<html> 
<head> 
<script type="text/javascript" language="javascript"> 
function Speak() { 
    document.getElementById("Speech").load(); 
    document.getElementById("Speech").play(); 
} 
</script> 
</head> 

<body> 
<audio id="Speech" src="speech.wav"></audio> 
<button type="button" onclick="Speak()">Speak</button> 
</body> 
</html> 

如果我改變speech.wav內容,瀏覽器仍然發揮在按鈕的點擊前面的內容。有沒有辦法重新讀取音頻文件?

回答

1

最傻瓜的方法是將某種類型的版本密鑰附加到URL上,例如隨機ID。

<audio id="speech" src="speech.wav?ver=8976234asdfasdf"> 

雖然有可能是另一種方法。

1

剛剛找到答案,這證實了我的懷疑:緩存。爲了防止瀏覽器緩存的頁面(包括音頻),我修改了代碼:

<html> 
<head> 
// Add these two meta tags in order to set the caching off and never 
// be cached again at expiration 
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="-1"> 
<script type="text/javascript" language="javascript"> 
function Speak() { 
    document.getElementById("Speech").load(); 
    document.getElementById("Speech").play(); 
} 
</script> 
</head> 

<body> 
<audio id="Speech" src="speech.wav"></audio> 
<button type="button" onclick="Speak()">Speak</button> 
</body> 
</html> 

現在它工作正常每次我改變speech.wav內容的時間。