HTML 5 video not playing

Viewed 1752

I have a simple form where a user can enter input a name and submit , when user enter the user name, automatically the video is played in iframe, when user click submit, I want to open a new page which contain a video automatically the video should play when user click submit.

Here is what I have gone so far:

Form page HTML.

<!DOCTYPE html>
<html>

<head>
    <title>Wideo iframe</title>
    <!-- Website Font style -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
        crossorigin="anonymous">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
    <!-- Google Fonts -->
    <link href='https://fonts.googleapis.com/css?family=Passion+One' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Oxygen' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="./css/style.css">
    <link rel="stylesheet" href="./css/form.css">

</head>

<body>

    <iframe id="videoframe" src="videoframe.html"></iframe>
    <br />
    <div class="container">
        <div class="row main">
            <div class="main-login main-center">
                <h5>WYP: Formularz.</h5>
                <form id="formData" name="contact" role="form" method="post" action="thankspage.html" target="_blank">

                    <div class="form-group">
                        <label for="name" class="cols-sm-2 control-label">Imie</label>
                        <div class="cols-sm-10">
                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
                                <input type="text" class="form-control" name="name" id="name" placeholder="Enter your Name" />
                            </div>
                        </div>
                    </div>

                    <input type="submit" id="button" value="submit" class="btn btn-primary btn-lg">


                </form>
            </div>
        </div>
    </div>



</body>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous"></script>
<script src="js/form.js" type="text/javascript"></script>

</html>

here is form.js

//form script

        $(document).ready(function () {
            $(window).resize(resizeIframe);
            $(window).resize();

            //play a video when user enter aa name
            $("input#name").on("change", function () {
                document.getElementById('videoframe').contentWindow.postMessage( "event=fieldchanged&fieldtype=" + "name" + "&value=" + $("input#name").val(), "*");
            });

            // play a video when a user clicks submit button this is not working !!!!!!!
            $("#button").on("click", function(){
              document.getElementById('videoframe').contentWindow.postMessage(
                  JSON.stringify({
                    event: 'submitSlide()'
                  }), "*")

            })


        });

        function resizeIframe() {
            console.log($("iframe#videoframe").width()*576/1024 );
            $("iframe#videoframe").height( $("iframe#videoframe").width()*576/1024 );

        }

here is videogframe.js

   var autoplay = true;


   var movieName1 = "./videos/videoplayback.mp4"
   var movieThanks = "./videos/darasa.mp4"



   function resizeFunction() {
       $("#videowrapper").height($("#videowrapper").width() * 576 / 1024);

   }

   window.addEventListener("message", receiveMessage, false);

   function receiveMessage(eventPosted) {

       var values = eventPosted.data.split("&");
       var event = values[0].split("=")[1];
       var fieldtype = values[1].split("=")[1];
       var value = values[2].split("=")[1];

       console.log(event, fieldtype, value);

       switch (event) {
           case "fieldchanged":

               switch (fieldtype) {

                       case "name":
                       openSlide("nameSlide", {
                           value: value
                       });

                       case  "submit":
                       openSlide("submitSlide", {
                           value: value,

                       });
                       break;

                   default:
                       break;
               }
               break;
           default:
               console.log("Event not supported");
               break;
       }

   }

   function openSlide(slideName, params) {
       switch (slideName) {
               case "nameSlide":
               openName(params);
               break;

               case "submitSlide":
               openSubmit(params);
               break;
       }

   }

   var params = null;

   function openName(_params) {
    playVideo(movieName1);
   }
   function  openSubmit(_params){
     playVideo(movieThanks);
   }


 // function top play video
   function playVideo(src) {

       $("#playervideo").attr("src", src);
       $("#playervideo")[0].muted = false;


       if (autoplay == true) {

           var playPromise = $("#playervideo")[0].play();

           if (playPromise !== undefined) {

               playPromise.then(function () {}).catch(function () {

                   if (autoplay == true) {
                       $("#video-unmute-button").addClass("show");
                       $("#playervideo")[0].muted = true;
                       var playPromise2 = $("#playervideo")[0].play();

                       playPromise2.then(function () {

                       }).catch(function () {
                           $("#video-start-button").addClass("show");


                           $("#video-start-button").on("click", function () {
                               $("#playervideo")[0].muted = false;
                               $("#playervideo")[0].play();
                               $("#video-start-button").removeClass("show");

                           });
                       });

                       console.log("pause force");
                   } else {

                   }
               });
           } else {}
       } else {

       }

   }

Here video frame html page.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Wideo iframe</title>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <link rel="stylesheet" href="css/style.css">

</head>

<body>
    <div id="videowrapper">
        <video  id="playervideo" controls></video>
        <canvas id="videocanvas" width="1024" height="576"></canvas>
        <div id="testform"><input type="text"/></div>
    </div>
</body>
<script src="jslibs/howler.min.js"></script>
<script src="https://code.createjs.com/1.0.0/createjs.min.js"></script>
<script src="js/videoframe.js" type="text/javascript"></script>

</html>

Am using post messege for communication.

Everything works as expected except when I click submit , it just open new page without playing a video .

UPDATE: please any one does not understand what I want , down

Here is repo for what I am trying to do : Video frame

just clone the repo run the page named formpage.html, start filling the form u will see automatically video plays, but when u click the submit button just opnes the page but the video is not playing : Test it your self

UPDATE: to be more specific I want to use post message like this by adding event listner.

  // play a video after the user click submit button
            $("input#button").on("click", function () {
                document.getElementById('videoframe').contentWindow.postMessage( "event=fieldchanged&fieldtype=" + "submit" + "&value=" + $("input#button").val(), "*");
            });

Why my video is not playing? what am I doing wrong? thanks.

1 Answers

The problem is actually very simple, it's not working because you're putting the video tag inside the iframe tag in your thankspage.html, remove the iframe tag and add the muted flag and it should work.

So change this

<iframe id="videoframe" src="videoframe.html">
  <video width="320" height="240" controls autoplay>
    <source src="./video/janusz.mp4" type="video/mp4">
  </video>
</iframe>

to this :

<video width="320" height="240" controls autoplay muted>
    <source src="./videos/janusz.mp4" type="video/mp4">
</video>

This will of course break your CSS, but you can easily manage to resize it accordingly afterwards by putting the video tag inside a div and manipulate the div.

As you can see here : https://jsfiddle.net/8xfq9urh/

Related