How to properly unload/destroy a VIDEO element

后端 未结 15 2038
闹比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:16

    This "solution" is reported to work, presumably because it would make those video container objects available for garbage collection (see the note below for a discussion of why delete shouldn't be making a difference). In any case, your results are likely to vary by browser:

    $(container_selector).children().filter("video").each(function(){
        this.pause(); // can't hurt
        delete this; // @sparkey reports that this did the trick (even though it makes no sense!)
        $(this).remove(); // this is probably what actually does the trick
    });
    $(container_selector).empty();
    

    Note: There's no doubt that the delete keyword is specified only to remove properties from objects (as others have pointed out in the comments). Logging this to the console both before and after the delete this line, above, shows the same result each time. delete this should do nothing and make no difference. Yet this answer continues to receive a trickle of votes, and people have reported that omitting delete this makes it stop working. Perhaps there's strangeness in how some browser JS engines implement delete, or an unusual interaction between a browser's delete and what jQuery is doing with this.

    So, just be aware, if this answer solves your problem, that if it does work, it's not clear why that's the case, and it's just as likely to stop working for any number of reasons.

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

    One solution that worked for me in AngularJS is using below code: In case you don't want to remove your source url, and reset to start of the video

    let videoElement = $document[0].getElementById('video-id');
    videoElement.pause();
    videoElement.seekable.start(0);
    videoElement.load();
    

    And in case you want to remove the source from video tag:

    let videoElement = $document[0].getElementById('video-id');
    videoElement.pause();
    videoElement.src="";
    videoElement.load();
    

    Hope someone finds it useful.

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

    This is what I did to solve this problem. I created 2 video elements (video1 & video2). After finished using video1, get the source(src) attribute value and then remove video1 from DOM.

    Then set video2 source (src) to whatever value you got from video1.

    Do not use stream from video1 as it is cached in memory.

    Hope this will help.

    0 讨论(0)
  • 2020-12-04 08:19
    delete(this); 
    

    is not a solution. If it worked for x or y it is a browser misbehaviour. Read here:

    The delete operator removes a property from an object.

    The truth is that some browsers (Firefox for example) will cache in memory the video buffer when autoplay property is on. It is a pain to deal with.

    Removing the video tag from the DOM or pausing it can only produce unstable results. You have to unload the buffer.

    var video = document.getElementById('video-id');
    video.src = "";
    

    My experiment shows that it is done as so but unfortunately this is browser implementation not completely specified by the spec. You do not need to call load() after src change. When changing the src of a video tag you implicitly call a load() on it, this is stated in the W3C spec.

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

    To reset the video to Blank without removing it

    $("#video-intro").first().attr('src','')
    

    It stops the video

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

    Just to clarify for anyone trying this later, the solution was this: (confirmed with h264 videos in Safari 5.0, untested in FF/opera yet)

    $(container_selector).children().filter("video").each(function(){
        this.pause();
        delete(this);
        $(this).remove();
    });
    $(container_selector).empty();
    
    0 讨论(0)
提交回复
热议问题