如何通过 javascript 更改 html5 视频的 src 而不会导致 chrome 崩溃或泄漏内存?

发布于 2024-10-12 02:10:05 字数 677 浏览 5 评论 0原文

我正在开发一个仅在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心碎的声音 2024-10-19 02:10:05

我也遇到了同样的问题。
我在一些论坛上读到这是 chrome 的内存泄漏错误(有人说这只是在 chrome 8 中发生,在 chrome 6 中工作正常,但我没有测试它)。

我还读到,睡眠有帮助。我尝试过,确实如此,如果我在更改 url 属性之前进行睡眠并调用 load() ,则崩溃次数会减少。但在多次更改后仍然继续崩溃。

然后,我尝试使用 setTimeout (与睡眠不同,它不会占用 CPU,从而可以自由地进行 chrome 的工作)。

现在它正在发挥作用。
尝试看看我的代码是否有帮助。

var videoChangingPending = false;
function changeMovieSource(url, title){

        var $video = $('#video');
        try {
            document.getElementById('video').src = url;
        }
        catch (e) {
            alert(e);
        }     

        $video.attr('autoplay', 'true');
        $video.data('currentTitle', title);

        document.getElementById('video').load();

        videoChangingPending = false;
}    

function startPlayer(url, title) {

        if(videoChangingPending == true) 
            return;



        document.getElementById('video').pause();


        videoChangingPending = true;
        var changeMovieCallback = function(){ changeMovieSource(url, title);}
        var t = setTimeout(changeMovieCallback, 800);

}

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.

var videoChangingPending = false;
function changeMovieSource(url, title){

        var $video = $('#video');
        try {
            document.getElementById('video').src = url;
        }
        catch (e) {
            alert(e);
        }     

        $video.attr('autoplay', 'true');
        $video.data('currentTitle', title);

        document.getElementById('video').load();

        videoChangingPending = false;
}    

function startPlayer(url, title) {

        if(videoChangingPending == true) 
            return;



        document.getElementById('video').pause();


        videoChangingPending = true;
        var changeMovieCallback = function(){ changeMovieSource(url, title);}
        var t = setTimeout(changeMovieCallback, 800);

}
倾听心声的旋律 2024-10-19 02:10:05

我讨厌所有这些答案,因为它们太短或依赖于其他框架。

这是执行此操作的“一种”vanilla JS 方法,在 Chrome 中工作,请在其他浏览器中测试:

http: //jsfiddle.net/mattdlockyer/5eCEu/2/

HTML:

<video id="video" width="320" height="240"></video>

JS:

var video = document.getElementById('video');
var source = document.createElement('source');

source.setAttribute('src', 'http://www.tools4movies.com/trailers/1012/Kill%20Bill%20Vol.3.mp4');

video.appendChild(source);
video.play();

setTimeout(function() {  
    video.pause();

   source.setAttribute('src', 'http://www.tools4movies.com/trailers/1012/Despicable%20Me%202.mp4'); 

    video.load();
    video.play();
}, 3000);

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:

<video id="video" width="320" height="240"></video>

JS:

var video = document.getElementById('video');
var source = document.createElement('source');

source.setAttribute('src', 'http://www.tools4movies.com/trailers/1012/Kill%20Bill%20Vol.3.mp4');

video.appendChild(source);
video.play();

setTimeout(function() {  
    video.pause();

   source.setAttribute('src', 'http://www.tools4movies.com/trailers/1012/Despicable%20Me%202.mp4'); 

    video.load();
    video.play();
}, 3000);
戏舞 2024-10-19 02:10:05

我认为第一种方法应该可以正常工作,而 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.

何以畏孤独 2024-10-19 02:10:05

我遇到了这个问题,所以我所做的就是删除视频标签,然后“即时”添加另一个具有当前网址的标签。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文