如何通过 javascript 更改 html5 视频的 src 而不会导致 chrome 崩溃或泄漏内存?
我正在开发一个仅在 Chrome 上运行的应用程序。
我需要能够从正在播放的视频切换源。
我使用 javascript (&jQuery) 来更改 src
属性:
$fullscreenVideo.get(0).src = '/video/' + name + '.mp4'; // crash here
$fullscreenVideo.get(0).load();
$fullscreenVideo.get(0).play();
它可以工作几次,但我的 chrome(尝试过 beta 和 dev 通道)最终崩溃(页面变得无响应)。
如果我尝试在最后一个代码块前面创建一个新元素:
$fullscreenVideo.remove();
$fullscreenVideo = $('<video id="video-fullscreen" width="800" height="600" loop="loop"></video>').appendTo("#page-fullscreen > div.fullscreen");
每个视频开关都会将 RAM 增加 20Mo,而不会降低。
有没有办法跟踪/防止 chrome 在 src 更新时崩溃? 有没有办法强制释放内存?
I'm working on an application running on Chrome only.
I need to be able to switch the source from a playing video.
I've used javascript (&jQuery) to change the src
attribute :
$fullscreenVideo.get(0).src = '/video/' + name + '.mp4'; // crash here
$fullscreenVideo.get(0).load();
$fullscreenVideo.get(0).play();
It works a few times but my chrome (tried beta & dev channels) ends up crashing (page become unresponsive).
If i try to create a new element prepending last codeblock with :
$fullscreenVideo.remove();
$fullscreenVideo = $('<video id="video-fullscreen" width="800" height="600" loop="loop"></video>').appendTo("#page-fullscreen > div.fullscreen");
Every video switch increase RAM by 20Mo without ever getting back down.
Is there a way to trace/prevent chrome crash en src update ?
Is there a way to force free memory ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也遇到了同样的问题。
我在一些论坛上读到这是 chrome 的内存泄漏错误(有人说这只是在 chrome 8 中发生,在 chrome 6 中工作正常,但我没有测试它)。
我还读到,睡眠有帮助。我尝试过,确实如此,如果我在更改 url 属性之前进行睡眠并调用 load() ,则崩溃次数会减少。但在多次更改后仍然继续崩溃。
然后,我尝试使用 setTimeout (与睡眠不同,它不会占用 CPU,从而可以自由地进行 chrome 的工作)。
现在它正在发挥作用。
尝试看看我的代码是否有帮助。
I've been the same problem.
I read in some foruns that is a chrome's bug with memory leaks (some people say that just happens in chrome 8 and in chrome 6 works fine, but i didn't test it).
I also read that using a sleep helps. I tried and it is true, if i put a sleep before change url atribute and call load() the crashes number decreases. But still continues to crash after many changes.
Then, i tried to use setTimeout (unlike sleep, it doesn't bloq CPU, leaving it free to chrome's work).
And it is working now.
Try to see if my code helps.
我讨厌所有这些答案,因为它们太短或依赖于其他框架。
这是执行此操作的“一种”vanilla JS 方法,在 Chrome 中工作,请在其他浏览器中测试:
http: //jsfiddle.net/mattdlockyer/5eCEu/2/
HTML:
JS:
I hated all these answers because they were too short or relied on other frameworks.
Here is "one" vanilla JS way of doing this, working in Chrome, please test in other browsers:
http://jsfiddle.net/mattdlockyer/5eCEu/2/
HTML:
JS:
我认为第一种方法应该可以正常工作,而 Chrome 的
实现仍然存在错误。请随意在他们的错误报告论坛上提及它。
I would assume the first way should work fine, and Chrome's
<video>
implementation is still buggy. Feel free to mention it on their bug report forums.我遇到了这个问题,所以我所做的就是删除视频标签,然后“即时”添加另一个具有当前网址的标签。
I was experiencing this problem so all I did was remove the video tag and then add another with the current url "on the fly".