I'm pausing a video using its pause()
method.. the problem is that the audio continues playing... I also tried pausing it from the Javascript Console in Firefox... nothing happens. The video is in .ogg format and is not even playing in Chrome (because I think it's not supported).
I hosted the video on Amazon S3 and it is streaming perfectly. I'm creating the element dynamically, loading its info from a JSON request.
Here is some code:
function showVideo() {
var video = videodata;
var videobox = $('#videobox').first();
var videoplayer = document.getElementById('videoplayer');
if (video.Enabled) {
if ((videoplayer != null && videoplayer.currentSrc != video.Location) || videoplayer == null) {
console.log('Creating video elem');
videobox.empty();
videobox.append('<video id="videoplayer" preload="auto" src="' +
video.Location + '" width="100%" height="100%" autoplay="autoplay" loop="loop" />');
videobox.show();
}
} else {
if (videoplayer != null) {
videoplayer.pause();
console.log('Pausing video...');
}
console.log('Deleting video elem');
videobox.hide();
videobox.empty();
}
}
I already posted a similar question before... but now I'm using other browsers, so I thought I have to create a new question.
Here is the working code (thanks to the user heff!)
function showVideo() {
var video = videodata;
var videobox = $('#videobox').first();
var videoplayer = document.getElementById('videoplayer');
if (video.Enabled) {
if ((videoplayer.src != video.Location) || videoplayer.src == '') {
console.log('Playing video: ' + video.Location);
videoplayer.src = video.Location;
videoplayer.load();
videoplayer.play();
videobox.show();
}
} else {
if (videoplayer.src != '') {
console.log('Pausing video...');
videoplayer.pause();
videoplayer.src = '';
videobox.hide();
}
}
}
The pause() method halts (pauses) the currently playing audio. Tip: This method is often used together with the play() method. Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio).
pause() The HTMLMediaElement. pause() method will pause playback of the media, if the media is already in a paused state this method will have no effect.
Complete HTML/CSS Course 2022 You need to set src attribute to identify the media source and include a controls attribute so the user can play and pause the media.
My guess would be that showVideo is being called twice some how and creating two copies, one of which keeps playing even after you call pause on the other.
In your code, the videoplayer var won't refer to the video tag you create later with append, it will point to whatever had that id before, which I'm assuming gets removed when you empty the box, but might stick around in memory (and continue to play sound).
Just my best guess, but either way it'd be better to use the video element's API to set the source and other parameters rather than emptying the box and rebuilding the tag.
videoplayer.src = video.Location;
videoplayer.autoplay = true;
// etc.
Also, 100% isn't a valid value for the width/height attributes. You'll need to use CSS to make the video stretch to fill an area.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With