How to properly unload/destroy a VIDEO element

后端 未结 15 2041
闹比i
闹比i 2020-12-04 07:58

I\'m working on a realtime media browsing/playback application that uses objects in the browser for playback, when available.

I\'m using a

相关标签:
15条回答
  • 2020-12-04 08:40

    It is very tricky to dispose video from the DOM structure. It may lead to browser crashing. Here is the solution that helped me in my project.

    var videoElement = document.getElementById('id_of_the_video_element_here');
    videoElement.pause();
    videoElement.removeAttribute('src'); // empty source
    videoElement.load();
    

    this will reset everything, silent without errors !

    Edit: Here are the full details as recommended in the Standard: https://html.spec.whatwg.org/multipage/media.html#best-practices-for-authors-using-media-elements

    Hope it resolve your query.

    0 讨论(0)
  • 2020-12-04 08:40

    This snippet doesn't do any effecient DOM manipulations (no tag removal) and doesn't fire error event for <video> unlike this answer:

    var video = document.getElementById('video');
    video.removeAttribute('src');
    video.load();
    

    Furthermore, it doesn't fire loadstart event. And it's like it should work - no video, no load start.

    Checked in Chrome 54 / FF 49.

    0 讨论(0)
  • 2020-12-04 08:40
    var video = document.getElementById('video');
            if (video.firstChild) {
                video.removeChild(video.firstChild);
                video.load();
            }
    
    0 讨论(0)
提交回复
热议问题