I would like to add custom controls to my html5 video player, so as to limit the number of manual replays of a video. Maybe the simplest solution would be to hide the play button after X amount of times it is selected, via jQuery/javascript code addition?
Hi @ChrisjChrisj, I don’t think you can hide the play button specifically – only the complete controls, which can be done by simply setting its controls = false:
const limitReplays = (videoElement, limit) => {
let count = 0
const handleProgress = () => {
if (videoElement.ended) {
count++
}
if (count === limit) {
// Hide the controls and clean up the event listener
videoElement.controls = false
videoElement.removeEventListener('timeupdate', handleProgress)
}
}
videoElement.addEventListener('timeupdate', handleProgress)
}
// Pass the desired video element(s):
document
.querySelectorAll('video')
.forEach(videoElement => limitReplays(videoElement, 5))
As for custom controls, there’s a thorough guide here on the MDN.
Well I don’t see any JS in your code… if you just copy / pasted my snippet to your page though then my guess would be that you included it in the head; you can only query elements that actually exist when the script runs, so it should be at the end of the body (or wrapped in a DOMContentReady event listener).
IDK it works for me… try adding breakpoints or console.log()s to see if the limitReplays() function gets called with the desired video elements, and subsequently handleProgress(). Also as there are some data-* attributes on your source elements, is there some other scripting at play that might interfere with it?