[英]How to store cache values of audio file?
我在网站上使用音频( .swf
)文件,该文件有两个选项:“ AUDIO ON”和“ AUDIO OFF”。 我可以打开和关闭音频。 问题是音频没有停止。 它反复播放。 谁能帮我?
另一个问题是,每当我重新加载任何页面时,音频文件都会从头开始播放。 我希望从以前停止播放音频的位置开始播放。
编辑:从注释中添加了代码
<audio id="main_audio" <?php echo $autoplaynf;?> preload="auto" loop="loop">
<embed hidden="true" autostart="true" loop="true" src="audio.mp3" />
</audio>
刷新页面时,音频将从头开始。 如果确实需要从离开的地方开始,则必须将播放位置存储在Cookie中。 也可以通过将位置存储在服务器上来完成此操作,但是如果在整个音频文件都已缓存时连接断开了,则该功能将无法正常工作。
timeupdate
事件将使您知道当前播放位置何时更改。 此播放位置应更改 前提是您具有以下代码:
<audio id="main_audio" <?php echo $autoplaynf;?> preload="auto" loop="loop">
<embed hidden="true" autostart="true" loop="true" src="audio.mp3" />
</audio>
您可以添加javascript(不确定是否使用jQuery)来执行上述操作。 请注意,只有在加载音频标签后才能执行此操作。 请注意,这只是伪代码。 尝试将其用作完成事情的准则。
// when page is loaded do the following actions
var atag = document.getElementById("main_audio");
atag.addEventListener("timeupdate", timeUpdateEvent, false);
// check if coockie exists
var timePos = readCookie();
atag.currentPos = timePos;
function timeUpdateEvent(event) {
// get time position from audio tag
// store cookie
}
function readCookie() {
// read the cookie and return time value
// of cookie does not exist return a timevalue
// that will let the audio start from the beginning
}
对于存储,读取和删除Cookie,我在SO上引用了此答案 。 也许有更多的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.