HTML5 video custom controls

Joseph Zammit

I'm trying to add some custom controls to my html 5 video but having issues getting it to work.

I need to add a toggle button Pause and Play. This is part of my code and the error I'm getting is Uncaught TypeError: Cannot set property 'title' of null

var billboardC, video, playpause;
var videoBGPlayer;

function init(){

    billboardC = document.getElementById('billboardC');

    video = document.getElementById('video-player');
    playpause = document.getElementById('playpause');
}


function OpenVideo(e){
    console.log('open video');
    billboardC.innerHTML += "<div class='closeButton'></div>";
    billboardC.innerHTML += "<a href='www.google.com' target='blank'     class='fombutton'>Find Out More</a>";
    billboardC.style.visibility = 'visible';
    billboardC.innerHTML += "<video id='video-player'     class='videoSquare' poster='' muted> <source src='my-video.mp4'     type='video/mp4' /></video><div id='controls'><button id='playpause'      onclick='togglePlayPause()' title='play'>Play</button></div>";

}

function togglePlayPause() {
    if (video.paused || video.ended) {
      playpause.title = "pause";
      playpause.innerHTML = "pause";
      video.play();
      console.log('play');
   }
   else {
      playpause.title = "play";
      playpause.innerHTML = "play";
      video.pause();
      console.log('pause');
   }
}


window.onload = function(){ init(); }

Thanks

zer00ne

Detailed descriptions are in the comments within the Snippet.

SNIPPET

    var billboardC = document.getElementById('billboardC');
    var videoBGPlayer;

/*
The variables that were within init() were not accessible by other functions. 

Normally you would just place the variables outside to become global and accessible. But all of the elements do not exist until OpenVideo() is finished parsing them, so the variables are undefined. 

In order to create useful references, I placed the needed variables within togglePlayPause().

*/
/* 
OpenVideo() was never invoked, therefore no HTML, no dice. So I placed OpenVideo within init() so it would be the first function (besides init() of course to be invoked.
*/
function init(){
    OpenVideo();
}

function OpenVideo(){
    console.log('open video');
    billboardC.innerHTML += "<div class='closeButton'></div>";
    billboardC.innerHTML += "<a href='www.google.com' target='_blank'     class='formbutton'>Find Out More</a>";
    billboardC.style.visibility = 'visible';
    billboardC.innerHTML += "<video id='video-player' class='videoSquare' poster='' muted> <source src='my-video.mp4' type='video/mp4'></video><div id='controls'><button id='playpause' onclick='togglePlayPause()' title='play'>Play</button></div>";

}

function togglePlayPause() {
  var video = document.getElementById('video-player');
  var playpause = document.getElementById('playpause');
    if (video.paused || video.ended) {
      playpause.title = "pause";
      playpause.innerHTML = "pause";
      video.play();
      console.log('play');
   }
   else {
      playpause.title = "play";
      playpause.innerHTML = "play";
      video.pause();
      console.log('pause');
   }
}

/*
When using window.onload = foo; do not use the functions parenthesis `()`. If you leave out the paranthesis, that will tell the renderer that it is up to it to decide when init should execute.
*/
window.onload = init;
<!--#billboardC didn't exsist and it wasn't included in OpenVideo()-->
<main id="billboardC"></main> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to disable iOS HTML5 video controls?

From Java

Streaming a video file to an html5 video player with Node.js so that the video controls continue to work?

From Dev

Custom HTML5 video player controls with AngularJS

From Dev

Styling HTML5 Video Controls

From Dev

Making HTML5 video draggable without losing the controls

From Dev

HTML5 video tag controls="false" - controls show on pause

From Dev

change html5 video controls layout when fullscreen

From Dev

How to prevent event bubbling when clicking on HTML5 video controls in Firefox

From Dev

HTML5 video & jQuery: show/hide controls on click

From Dev

How to Add HTML5 Video controls dynamically

From Dev

Video streaming html5 player. how to automate sliding seek & volume controls

From Dev

Soundcloud API not working with custom HTML5 audio player with JS controls, works with HTML5 controls

From Dev

Adding controls to HTML5 background video with CSS

From Dev

HTML5 video custom controls for any number of videos on the page

From Dev

HTML5 Video Controls do not work

From Dev

How to display custom video controls even in fullscreen

From Dev

Styling HTML5 Video Controls

From Dev

html5 video: play/pause custom button issue

From Dev

Keep custom video controls out of sight

From Dev

customising the buttons availabe in the controls element of the html5 video player

From Dev

Video streaming html5 player. how to automate sliding seek & volume controls

From Dev

Controls not showing on HTML5 Video when using javascript

From Dev

HTML5 video custom controls for any number of videos on the page

From Dev

javascript video custom controls

From Dev

HTML5 video custom controls

From Dev

How can I hide my custom html5 video controls when the user is inactive?

From Dev

html5 video doesn't play on android without controls

From Dev

Indexing Icons in multiple Custom HTML5 Video Controls

From Dev

How to make the video of html5 controls to be visible always?

Related Related

  1. 1

    How to disable iOS HTML5 video controls?

  2. 2

    Streaming a video file to an html5 video player with Node.js so that the video controls continue to work?

  3. 3

    Custom HTML5 video player controls with AngularJS

  4. 4

    Styling HTML5 Video Controls

  5. 5

    Making HTML5 video draggable without losing the controls

  6. 6

    HTML5 video tag controls="false" - controls show on pause

  7. 7

    change html5 video controls layout when fullscreen

  8. 8

    How to prevent event bubbling when clicking on HTML5 video controls in Firefox

  9. 9

    HTML5 video & jQuery: show/hide controls on click

  10. 10

    How to Add HTML5 Video controls dynamically

  11. 11

    Video streaming html5 player. how to automate sliding seek & volume controls

  12. 12

    Soundcloud API not working with custom HTML5 audio player with JS controls, works with HTML5 controls

  13. 13

    Adding controls to HTML5 background video with CSS

  14. 14

    HTML5 video custom controls for any number of videos on the page

  15. 15

    HTML5 Video Controls do not work

  16. 16

    How to display custom video controls even in fullscreen

  17. 17

    Styling HTML5 Video Controls

  18. 18

    html5 video: play/pause custom button issue

  19. 19

    Keep custom video controls out of sight

  20. 20

    customising the buttons availabe in the controls element of the html5 video player

  21. 21

    Video streaming html5 player. how to automate sliding seek & volume controls

  22. 22

    Controls not showing on HTML5 Video when using javascript

  23. 23

    HTML5 video custom controls for any number of videos on the page

  24. 24

    javascript video custom controls

  25. 25

    HTML5 video custom controls

  26. 26

    How can I hide my custom html5 video controls when the user is inactive?

  27. 27

    html5 video doesn't play on android without controls

  28. 28

    Indexing Icons in multiple Custom HTML5 Video Controls

  29. 29

    How to make the video of html5 controls to be visible always?

HotTag

Archive