Skip to content

Instantly share code, notes, and snippets.

@danro
Created June 6, 2013 23:33
Show Gist options
  • Save danro/5725870 to your computer and use it in GitHub Desktop.
Save danro/5725870 to your computer and use it in GitHub Desktop.
Remove HTML5 video and clear src attribute to prevent leaks.
// remove audio + video + stop all the downloadin’
// assumes $video and $audio are jQuery selectors for <video> and <audio> tags.
var removeMedia = function () {
_.each([$video, $audio], function ($media) {
if (!$media.length) return;
$media[0].pause();
$media[0].src = '';
$media.children('source').prop('src', '');
$media.remove().length = 0;
});
};
@absyah
Copy link

absyah commented Oct 20, 2017

alternatively we can do this as well (for video player):

$('video source').each(function(num,val){
    $(this).attr('src', '')
});

@Beraliv
Copy link

Beraliv commented Dec 12, 2019

alternatively we can do this as well (for video player):

$('video source').each(function(num,val){
    $(this).attr('src', '')
});

It will produce MEDIA_ELEMENT_ERROR
Better to add removeAttribute after that: https://twitter.com/beraliv/status/1205214277956775936

@rhigdon
Copy link

rhigdon commented Feb 28, 2020

I additionally had to call load on the video object

videoPlayer.src = '';
videoPlayer.load();

@ddfridley
Copy link

@Beraliv

It will produce MEDIA_ELEMENT_ERROR
Better to add removeAttribute after that: https://twitter.com/beraliv/status/1205214277956775936

Thanks! This was really useful.

@Beraliv
Copy link

Beraliv commented Apr 28, 2022

@eladkarako Yeah, definitely a necessary thing especially for slow performant devices (e.g. Tizen 2.3 and 2.4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment