javascript - 将 Audio 对象设置为未定义是否会导致内存泄漏?

标签 javascript html audio html5-audio

我目前正在编写一个使用 HTML5 音频 API 的应用程序。在 Chrome、IE 和 Firefox 中,我注意到我可以创建一个 Javascript 音频对象,将其设置为播放声音文件,然后将其设为未定义,声音仍然会播放,如以下示例所示:

var a = new Audio;
a.src = 'longAudioFile.mp3';
a.play();
a = undefined;

由于我以类似的方式处理许多音频对象,如果我将一个音频对象设置为未定义,是否会导致内存泄漏,或者浏览器会在播放完毕/设置为暂停时清理它吗?

最佳答案

根据规范:

Media elements must not stop playing just because all references to them have been removed; only once a media element is in a state where no further audio could ever be played by that element may the element be garbage collected.

It is possible for an element to which no explicit references exist to play audio, even if such an element is not still actively playing: for instance, it could have a current media controller that still has references and can still be unpaused, or it could be unpaused but stalled waiting for content to buffer.

Playing the Media Resource

还有清理说明:

<...> to release resources held by media elements when they are done playing, either by being very careful about removing all references to the element and allowing it to be garbage collected, or, even better, by removing the element's src attribute and any source element descendants, and invoking the element's load() method.

Best practices for authors using media elements

关于javascript - 将 Audio 对象设置为未定义是否会导致内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24256776/

相关文章:

javascript - jQuery - 数据属性引号问题

JavaScript:删除事件监听器

javascript - 我如何确定是什么在初始页面加载时从元素中删除了 CSS 类?

audio - 在 Cocos2D-X 中尝试启用音乐暂停时出现 "SimpleAudioEngine has not been declared"错误

objective-c - 音频队列 |在 audioQueue 运行时更改音频设备

javascript - Sidr.js 菜单在桌面上禁用

javascript - 对没有数据绑定(bind)的选择列表进行 knockout 验证 'option' 或 'caption'

android - 触摸屏选择选项的解决方案(html选择标签)

javascript - 将html表格导入html页面

Android 流式传输 Wav 音频错误 : MediaPlayer Prepare failed: status=0x1