Html5 video controllers

Asked

Viewed 199 times

0

I have in my code a video that was placed with the following tags:

 <div id="video">
       <img src="images/bg-video.png" id="bg-video">
            <video  width="100%" height="100%" loop>
            <source src="video/animacao-lol.mp4" type="video/mp4" />
       </video>
 </div>

however as soon as the page loads the video starts to run with auto sound but I do not want to use the controls that is already available in the video tag I want to style my video with my own controllers that would be Play and volume and also wanted my video to start "mutated" not to scare the user when he enters the page follows the image of how I wanted my controllers :

inserir a descrição da imagem aqui

And so more or less I would have to keep the play button when I click on it to pop the pause button and so on if it is possible to do this please help me.

code I’m using:

video tag:

<div id="video">
           <img src="images/bg-video.png" id="bg-video">
                <video  width="100%" height="100%" loop>
                <source src="video/animacao-lol.mp4" type="video/mp4" />
           </video>
     </div>

js of the video

function init() {        // Master function, encapsulates all functions
        var video = document.getElementById("Video1");                                               
        if (video.canPlayType) {   // tests that we have HTML5 video support
            // if successful, display buttons and set up events
            document.getElementById("buttonbar").style.display = "block";                
            document.getElementById("inputField").style.display = "block";

            // helper functions
            //  play video
            function vidplay(evt) {
                if (video.src == "") {  // inital source load
                    getVideo();
                }
                button = evt.target; //  get the button id to swap the text based on the state                                    
                if (video.paused) {   // play the file, and display pause symbol
                    video.play();
                    button.textContent = "||";
                } else {              // pause the file, and display play symbol  
                    video.pause();
                    button.textContent = ">";
                }
            }
            //  load video file from input field
            function getVideo() {
                var fileURL = document.getElementById("videoFile").value;  // get input field                    
                if (fileURL != "") {
                    video.src = fileURL;
                    video.load();  // if HTML source element is used
                    document.getElementById("play").click();  // start play
                } else {
                    errMessage("Enter a valid video URL");  // fail silently
                }
            }



            //  button helper functions 
            //  skip forward, backward, or restart
            function setTime(tValue) {
            //  if no video is loaded, this throws an exception 
                try {
                    if (tValue == 0) {
                        video.currentTime = tValue;
                    }
                    else {
                        video.currentTime += tValue;
                    }

                 } catch (err) {
                     // errMessage(err) // show exception
                 errMessage("Video content might not be loaded");
                   }
         }
            //  display an error message 
            function errMessage(msg) {
            // displays an error message for 5 seconds then clears it
                document.getElementById("errorMsg").textContent = msg;
                setTimeout("document.getElementById('errorMsg').textContent=''", 5000);
            }
            // change volume based on incoming value 
            function setVol(value) {
                var vol = video.volume;
                vol += value;
                //  test for range 0 - 1 to avoid exceptions
                if (vol >= 0 && vol <= 1) {
                    // if valid value, use it
                    video.volume = vol;
                } else {
                    // otherwise substitute a 0 or 1
                    video.volume = (vol < 0) ? 0 : 1;                        
                }
            }
            //  button events               
            //  Play
            document.getElementById("play").addEventListener("click", vidplay, false);
            //  Restart
            document.getElementById("restart").addEventListener("click", function () {
                setTime(0);
            }, false);
            //  Skip backward 10 seconds
            document.getElementById("rew").addEventListener("click", function () {
                setTime(-10);
            }, false);
            //  Skip forward 10 seconds
            document.getElementById("fwd").addEventListener("click", function () {
                setTime(10);
            }, false);
            //  set src == latest video file URL
            document.getElementById("loadVideo").addEventListener("click", getVideo, false);

            // fail with message 
            video.addEventListener("error", function (err) {
                errMessage(err);
            }, true);
            // volume buttons
            document.getElementById("volDn").addEventListener("click", function () {
                setVol(-.1); // down by 10%
            }, false);
            document.getElementById("volUp").addEventListener("click", function () {
                setVol(.1);  // up by 10%
            }, false);

            // playback speed buttons
            document.getElementById("slower").addEventListener("click", function () {
                video.playbackRate -= .25;
            }, false);
            document.getElementById("faster").addEventListener("click", function () {
                video.playbackRate += .25;
            }, false);
            document.getElementById("normal").addEventListener("click", function () {
                video.playbackRate = 1;
            }, false);
            document.getElementById("mute").addEventListener("click", function (evt) {
                if (video.muted) {
                    video.muted = false;
                    evt.target.innerHTML = "<img alt='volume on button' src='vol2.png' />"
                } else {
                    video.muted = true;
                    evt.target.innerHTML = "<img alt='volume off button' src='mute2.png' />"
                }
            }, false);
        } // end of runtime
    }// end of master          

this statement does not work because I took it from example but it is not working

  • did you even search on google? There are 6 tutorials on the first page of the query only... http://lmgtfy.com/? q=Html5+video+custom+Controls

  • yes but it didn’t work

  • great so like this, rephrases your question this way: Put the codes that you tried (already did, but I believe you have more), says that you followed the tutorial Y (passes the link) and that in implementing X it did not work. With your question formulated this way it will be much easier to get good answers, because the question will be well formulated!

  • Ready friend this edited

1 answer

2

I can’t erase my question but I managed to do what I wanted I’ll leave that as an answer If anyone ever needs it:

the HTML:

            <h1 class="nomes">Lorem ipsum <strong class="slidText">Dolor</strong></h1>
            <p class="texto1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at risus neque. <br>Cras sit amet ligula 
                ut justo commodo porta id ut enim. Nulla est lectus, mollis sit amet vehicula id, volutpat eget mauris.</p>


            <div id="buttonbar" style="display: none;">

                <button id="play" title="Play button"></button>
                <button id="volDn"  title="Volume down button">-</button>
                <button id="volUp"  title="Volume up button">+</button>
                <button id="mute" title="Mute button" ><img src="images/video/demultado.png"/></button>        
            </div>   

            <center>
                <ul class="actions">
                    <li><a href="#one" class="botao-circulo scrolly"><img src="images/seta-baixo.png"></a></li>
                </ul>
            </center>
        </div>
    </section>

o js:

function init() {        // Master function, encapsulates all functions
    var video = document.getElementById("Video1");
    if (video.canPlayType) {   // tests that we have HTML5 video support
        // if successful, display buttons and set up events
        document.getElementById("buttonbar").style.display = "block";



        //  play video
        function vidplay(evt) {
            if (video.src == "") {  // inital source load
                getVideo();
            }
            button = evt.target; //  get the button id to swap the text based on the state                                    
            if (video.paused) {   // play the file, and display pause symbol
                video.play();
                button.textContent = "||";
            } else {              // pause the file, and display play symbol  
                video.pause();
                button.textContent = ">";
            }
        }



        // change volume based on incoming value 
        function setVol(value) {
            var vol = video.volume;
            vol += value;
            //  test for range 0 - 1 to avoid exceptions
            if (vol >= 0 && vol <= 1) {
                // if valid value, use it
                video.volume = vol;
            } else {
                // otherwise substitute a 0 or 1
                video.volume = (vol < 0) ? 0 : 1;
            }
        }


        //  button events               
        //  Play


        document.getElementById("play").addEventListener("click", vidplay, false);

        // volume buttons
        document.getElementById("volDn").addEventListener("click", function () {
            setVol(-.1); // down by 10%
        }, false);
        document.getElementById("volUp").addEventListener("click", function () {
            setVol(.1);  // up by 10%
        }, false);


        document.getElementById("mute").addEventListener("click", function (evt) {
            if (video.muted) {
                video.muted = false;
                evt.target.innerHTML = "<img alt='volume on button' src='../images/video/mutado.png' id='js-imagem' />"
            } else {
                video.muted = true;
                evt.target.innerHTML = "<img alt='volume off button' src='../images/video/desmutado.png' />"
            }
        }, false);
    } // end of runtime
}// end of master

Browser other questions tagged

You are not signed in. Login or sign up in order to post.