Reputation: 167
I'm trying to make my own custom controls for HTML5 video. So far I've only implemented a play/pause button, but I've run into a problem.
The video does not play at startup, so when I click the custom play button, I can hear the video's audio - but the video is stuck at the poster image.
I have a JSFiddle of my demo with the problem (try clicking the play button): https://jsfiddle.net/9gpg6gbd/
This is the snippet of the play/pause button's code:
// Initialize play button
if (video.paused) {
container.querySelector("#supervideo-playbutton").innerHTML = "►"
} else {
container.querySelector("#supervideo-playbutton").innerHTML = "| |"
}
container.querySelector("#supervideo-playbutton").addEventListener("click", function(){
if (video.paused) {
video.play();
container.querySelector("#supervideo-playbutton").innerHTML = "| |"
} else {
video.pause();
container.querySelector("#supervideo-playbutton").innerHTML = "►"
}
});
Any help would be much appreciated, I'm stumped on this one.
Upvotes: 2
Views: 1592
Reputation: 825
You have to errors:
Replace your video.play()
with the selector.
function initializeControls(container, video) {
// Initialize play button
if (video.paused) {
container.querySelector("#supervideo-playbutton").innerHTML = "►"
} else {
container.querySelector("#supervideo-playbutton").innerHTML = "| |"
}
container.querySelector("#supervideo-playbutton").addEventListener("click", function(){
if (document.body.querySelectorAll(".supervideo")[0].paused) {
document.body.querySelectorAll(".supervideo")[0].play();
container.querySelector("#supervideo-playbutton").innerHTML = "| |"
} else {
document.body.querySelectorAll(".supervideo")[0].pause();
container.querySelector("#supervideo-playbutton").innerHTML = "►"
}
});
}
function createVidElement() {
var videos = document.body.querySelectorAll(".supervideo");
[].forEach.call(videos, function(video) {
// Hide controls if the player has tdem
if (video.hasAttribute("controls")) {
video.controls = false;
}
// Create video container
var container = document.createElement('div');
container.setAttribute("class", "supervideo-container");
video.parentElement.appendChild(container);
container.appendChild(video);
// Created media controls
container.innerHTML = container.innerHTML + '<table cellpadding="0" cellspacing="0" id="supervideo-controls"><tr> <td><button id="supervideo-playbutton" type="button">×</button></td> <td>Time</td> <td>Scrollbar</td> <td>Mute button</td> <td>Volume</td> <td>Fullscreen</td> </tr></table>';
initializeControls(container, video);
});
}
createVidElement();
<video width="100%" controls class="supervideo">
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" />
</video>
Inside the foreach you changed the position of the <Video>
tag. you appended it to the new container. after this line your selector will be invalid, you have to reassign your selector.
function initializeControls(container, video) {
// Initialize play button
if (video.paused) {
container.querySelector("#supervideo-playbutton").innerHTML = "►"
} else {
container.querySelector("#supervideo-playbutton").innerHTML = "| |"
}
container.querySelector("#supervideo-playbutton").addEventListener("click", function(){
if (video.paused) {
video.play();
container.querySelector("#supervideo-playbutton").innerHTML = "| |"
} else {
video.pause();
container.querySelector("#supervideo-playbutton").innerHTML = "►"
}
});
}
function createVidElement() {
var videos = document.body.querySelectorAll(".supervideo");
[].forEach.call(videos, function(video) {
// Hide controls if the player has tdem
if (video.hasAttribute("controls")) {
video.controls = false;
}
// Create video container
var container = document.createElement('div');
container.setAttribute("class", "supervideo-container");
video.parentElement.appendChild(container);
//container.appendChild(video);
// Created media controls
container.innerHTML = container.innerHTML + '<table cellpadding="0" cellspacing="0" id="supervideo-controls"><tr> <td><button id="supervideo-playbutton" type="button">×</button></td> <td>Time</td> <td>Scrollbar</td> <td>Mute button</td> <td>Volume</td> <td>Fullscreen</td> </tr></table>';
initializeControls(container, video);
});
}
createVidElement();
<video width="100%" controls class="supervideo">
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" />
</video>
Upvotes: 1