问题描述
我有一个使用Audio接口播放任何给定声音的js函数(为每个调用创建一个新实例)。
这很好,直到第32次电话(有时更少)。此问题与Audio实例的发布直接相关。我知道这一点,因为我已经允许Chromium中的GC运行,并且可以让我再次播放32个左右的声音。
下面是一个示例我在做:
< html>< head>
< script type =text / javascript>
函数playSound(url){
var snd = new Audio(url);
snd.play();
snd = null;
}
< / script>
< / head>
< body>
< a href =#onclick =playSound('blah.mp3');>播放声音< / a>
< / body>< / html>
我也有这个功能,对于少于32个playSound调用的页面来说效果很好:
var AudioPlayer = {
cache:{},
play:function(url){
if (!AudioPlayer.cache [url])
AudioPlayer.cache [url] = new Audio(url);
AudioPlayer.cache [url] .play();
}
};
但是这不适用于我想要做的事情(动态地用其他内容替换div单独的文件),其中有更多的声音 - 1.内存使用会很容易上升,2.许多声音永远不会播放)。
我需要一种释放方式立即声音。是否有可能做到这一点?我没有发现音频界面的免费/关闭/卸载方法。
页面将在本地查看,因此持续加载声音并不是一个很大的因素(大部分声音都比较短)。
这不是一个详尽的答案,而是有什么方法强制chrome js引擎做垃圾回收?,:
更新:但是,由于@ 在下面的注释中指出,此标志只能在调试版本中使用。
I have a js function for playing any given sound using the Audio interface (creating a new instance for every call).
This works quite well, until about the 32nd call (sometimes less). This issue is directly related to the release of the Audio instance. I know this because I've allowed time for the GC in Chromium to run and it will allow me to play another 32 or so sounds again.
Here's an example of what I'm doing:
<html><head>
<script type="text/javascript">
function playSound(url) {
var snd = new Audio(url);
snd.play();
snd = null;
}
</script>
</head>
<body>
<a href="#" onclick="playSound('blah.mp3');">Play sound</a>
</body></html>
I also have this, which works well for pages that have less than 32 playSound calls:
var AudioPlayer = {
cache: {},
play: function(url) {
if (!AudioPlayer.cache[url])
AudioPlayer.cache[url] = new Audio(url);
AudioPlayer.cache[url].play();
}
};
But this will not work for what I want to do (dynamically replace a div with other content (from separate files), which have even more sounds on them - 1. memory usage would easily skyrocket, 2. many sounds will never play).
I need a way to release the sound immediately. Is it possible to do this? I have found no free/close/unload method for the Audio interface.
The pages will be viewed locally, so the constant loading of sounds is not a big factor at all (and most sounds are rather short).
This is not an exhaustive answer, but to the question "Is there any way to force the chrome js engine to do garbage collection?", a chromium.org guy replied:
UPDATE: However, as @plash noted in a comment below, this flag will only work in debug builds.
这篇关于JavaScript强制GC收集? /强制释放对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!