2016-09-29 11 views
2

私はコードを使用していますhere、私はボリュームボタンなどを取り除くためにちょっとハッキングしました。それぞれの自動再生、ループ、および再生の速度を変更するためのボタンがあります。任意の数のビデオ要素に対して機能するようにこのコードを変更するにはどうすればいいですか?init()関数などに複数の変数を指定していますか?JavaScriptを使用したHTML5ビデオ - 複数の動画のカスタムボタン

<html> 
    <head> 
    <title>Full player example</title> 
    <!-- Uncomment the following meta tag if you have issues rendering this page on an intranet or local site. -->  
    <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"/> --> 

    <script type="text/javascript"> 
     function init() {  // Master function, encapsulates all functions 
      var video = document.getElementById("Video1");            
      if (video.canPlayType) { // tests that we have HTML5 video support 
       document.getElementById("buttonbar").style.display = "block";     
       video.src == ""; 
       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 = "/lib/exe/fetch.php?media=experiment_log_books:sig0700.mp4"; // 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 
        } 
       } 

       // skip forward, backward, or restart 
       function setTime(tValue) { 
        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); 
       } 
       // 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); 

       // 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("play").click(); 
      } // end of runtime 
     }// end of master   
    </script> 

    </head> 
    <body onload="init();" >   

    <video id="Video1" controls autoplay loop height="320" width="640" title="Sig 0700">    
     HTML5 Video is required for this example 
    </video> 

    <div id="buttonbar" style="display: none;")> 
     <button id="restart" title="Restart button">[]</button> 
     <button id="slower" title="Slower playback button">-</button> 
     <button id="rew" title="Rewind button" >&lt;&lt;</button> 
     <button id="play" title="Play button">&gt;</button> 
     <button id="fwd" title="Forward button" >&gt;&gt;</button> 
     <button id="faster" title="Faster playback button">+</button> 
     <button id="normal" title="Reset playback rate button">=</button>   
    </div>  
    <div id= "inputField" style="display:none;" > 
     <label>Type or paste a video URL: <br/> 
     <input type="text" id="videoFile" style="width: 300px;" title="video file input field" value="http://ie.microsoft.com/testdrive/ieblog/2011/nov/pp4_blog_demo.mp4" />   
     <button id="loadVideo" title="Load video button" >Load</button> 
     </label> 
    </div> 
    <div title="Error message area" id="errorMsg" style="color:Red;"></div> 
    </body> 
</html> 
+0

すべてのビデオを同期しますか? – Piou

+0

いいえ、それぞれ個別のコントロール。 – James

+0

同じページのすべての動画が同時に表示されますか? – theoretisch

答えて

2

は、これを達成することができる多くの方法がありますが、あなたはそれがビデオの任意の数のために仕事をしたいので、私はマークアップの作成を入れて、動画のURLなどを受け付ける関数へのコントロールのバインディングう引数:次のようなもの:

<html> 
    <head> 
    <title>Full player example</title> 
    <!-- Uncomment the following meta tag if you have issues rendering this page on an intranet or local site. -->  
    <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"/> --> 

    <script type="text/javascript"> 
     window.videos = 0; 
     function createVideo (src) { 
      var container = document.createElement('div'), 
        video = document.createElement('video'), 
        buttonContainer = document.createElement('div'), 
        faster = document.createElement('button'), 
        forward = document.createElement('button'), 
        normal = document.createElement('button'), 
        playPause = document.createElement('button'), 
        restart = document.createElement('button'), 
        rewind = document.createElement('button'), 
        slower = document.createElement('button'), 
        error = document.createElement('div'), 
        showError = function (message) { 
         error.textContent = message; 
         setTimeout(function() { 
          error.textContent = ''; 
         }, 5000); 
        }; 

      ++window.videos; 

      document.body.appendChild(container); 
      container.id = 'container-' + window.videos; 

      container.appendChild(video); 
      video.id = 'video-' + window.videos; 
      video.autoplay = true; 
      video.controls = true; 
      video.loop = true; 
      video.src = src; 
      video.load(); 

      video.addEventListener('error', function (err) { 
       showError(err); 
      }, true); 

      container.appendChild(buttonContainer); 
      buttonContainer.id = 'buttonContainer-' + window.videos; 

      buttonContainer.appendChild(restart); 
      buttonContainer.appendChild(slower); 
      buttonContainer.appendChild(rewind); 
      buttonContainer.appendChild(playPause); 
      buttonContainer.appendChild(forward); 
      buttonContainer.appendChild(faster); 
      buttonContainer.appendChild(normal); 

      faster.id = 'faster-' + window.videos; 
      faster.textContent = '+'; 
      faster.title = 'Faster playback button'; 
      faster.addEventListener('click', function() { 
       video.playbackRate += .25; 
      }, false); 

      forward.id = 'forward-' + window.videos; 
      forward.textContent = '>>'; 
      forward.title = 'Forward button'; 
      forward.addEventListener('click', function() { 
       video.currentTime += 10; 
      }, false); 

      normal.id = 'normal-' + window.videos; 
      normal.textContent = '='; 
      normal.title = 'Reset playback rate button'; 
      normal.addEventListener('click', function() { 
       video.playbackRate = 1; 
      }, false); 

      playPause.id = 'playPause-' + window.videos; 
      playPause.textContent = '||'; 
      playPause.title = 'Play button'; 
      playPause.addEventListener('click', function (evt) { 
       button = evt.target; 
       if (video.paused) { 
        video.play(); 
        button.textContent = '||'; 
       } 
       else { 
        video.pause(); 
        button.textContent = '>'; 
       } 
      }, false); 

      restart.id = 'restart-' + window.videos; 
      restart.textContent = '[]'; 
      restart.title = 'Restart button'; 
      restart.addEventListener('click', function() { 
       video.currentTime = 0; 
      }, false); 

      rewind.id = 'rewind-' + window.videos; 
      rewind.textContent = '<<'; 
      rewind.title = 'Rewind button'; 
      rewind.addEventListener('click', function() { 
       video.currentTime -= 10; 
      }, false); 

      slower.id = 'slower-' + window.videos; 
      slower.textContent = '-'; 
      slower.title = 'Slower playback button'; 
      slower.addEventListener('click', function() { 
       video.playbackRate -= .25; 
      }, false); 

      container.appendChild(error); 
      error.id = 'error-' + window.videos; 
      error.title = 'Error message area'; 
      error.style.color = 'red'; 
     }; 

     function init() { 
      createVideo('/lib/exe/fetch.php?media=experiment_log_books:sig0700.mp4'); 
      createVideo('/lib/exe/fetch.php?media=experiment_log_books:sig0700.mp4'); 
     } 
    </script> 

    </head> 
    <body onload="init();"></body> 
</html> 
+0

あなたの答えをありがとう。私は今それを動作させる時間はありませんが、有効期限が切れる前に賞金のために受け入れられた答えをマークします。 – James

関連する問題