How do I stop audio from continuing to play after removing video tag with jQuery?

Go To StackoverFlow.com

8

The audio keeps playing when I use the remove function to remove the video. This doesn't happen if I don't include autoplay as an attribute in the video tag. I tried different things and nothing seems to work.

//$("#video").pause();
//$("#video").stop();
$("#video").empty();
$("#video").remove();
2012-04-05 21:46
by Orb Hitter
It works for me (tested in FF + Chrome): http://jsfiddle.net/CFMHQ/ , what browser are you using - Alex 2012-04-05 21:56
I'm using the current Firefox on a P - Orb Hitter 2012-04-05 21:56
I tried your code, it works fine... wonder if it's because I'm using append? $("#visual").append("") - Orb Hitter 2012-04-05 22:05
hmmm, that is interesting. if we add the video to the DOM with jQuery, it does not work. I can't even pause the video with the controls: http://jsfiddle.net/CFMHQ/1/ EDIT: Looks like this is the same problem: http://stackoverflow.com/questions/5927573/html5-video-playing-twice-audio-doubled-with-jquery-appen - Alex 2012-04-05 22:07
it also seems to do strange things to the audio - sometimes it echos or the volume change - Orb Hitter 2012-04-05 22:10
Yes, it's very strange. Take a look at this question, seems to be the same problem: http://stackoverflow.com/questions/5927573/html5-video-playing-twice-audio-doubled-with-jquery-appen - Alex 2012-04-05 22:11
To the musician in me this sounds quite interesting, especially the echos and volume changes. Can you tape it? ; - marue 2012-04-05 22:21
Yep, same exact problem, thanks for the link. I'm using $("video").trigger("play"); instead of autoplay now and it seems to work okay as a workaround - Orb Hitter 2012-04-05 22:23


4

Might be a little late, but I had the same issue and the above answer didn't worked for me (yes the tag was created by jQuery)

I got it working by using

jQuery('#myvideoTag').trigger('pause');
2012-06-30 12:29
by John Do


1

firsly try to create "video" tag empty in the html and create "source" tag into javascript code

<html>
.
.
<video id="main-video" autoplay=""></video>
.
.
</html>


<script>
   $('#main-video').append('<source type="video/mp4" src="URL.mp4">');
</script>
2017-09-19 10:50
by Cristian Triviño


0

The pause function that you're trying to call (first commented out line) exists on the DOM object, not the jQuery object.

You can get the DOM object through jQuery's get function, example: $('element').get()[0].

http://jsfiddle.net/calvintennant/bTmHn/

2013-02-23 19:21
by calvintennant
Ads