当前位置:Gxlcms > html代码 > 使用jQuery控制HTML5视频播放/暂停

使用jQuery控制HTML5视频播放/暂停

时间:2021-07-01 10:21:17 帮助过:58人阅读

我尝试用jQuery控制HTML5视频,两个视频分别在两个tab中,我希望点中tab后,该tab里的视频可以立即播放,而另外tab里的视频能够停止。

我的代码是这样的:

  1. $('#playMovie1').click(function(){
  2. $('#movie1').play();
  3. });

但发现这样不行,而用以下的js是可以的:

  1. document.getElementById('movie1').play();

解决方法:

play并不是jQuery的函数,而是DOM元素的函数,所以我们需要通过DOM来调用play,代码如下:

  1. $('#videoId').get(0).play();

最简单的方法实现Play和Pause:

  1. $('video').trigger('play');
  2. $('video').trigger('pause');

点击视频就能播放和暂停

  1. $("video").trigger("play");//for auto play
  2. $("video").addClass('pause');//for check pause or play add a class
  3. $('video').click(function() {
  4. if ($(this).hasClass('pause')) {
  5. $("video").trigger("play");
  6. $(this).removeClass('pause');
  7. $(this).addClass('play');
  8. } else {
  9. $("video").trigger("pause");
  10. $(this).removeClass('play');
  11. $(this).addClass('pause');
  12. }
  13. })

静音和取消静音

  1. $('body').find("video").attr('id', 'video')
  2. var myVid = document.getElementById("video");
  3. $('.sound-icon').click(function() {
  4. //here "sound-icon" is a anchor class.
  5. var sta = myVid.muted;
  6. if (sta == true) {
  7. myVid.muted = false;
  8. } else {
  9. myVid.muted = true;
  10. }
  11. })

HTML 5中播放视频的方法:

  1. Try this page in Safari 4! Or you can
  2. download the video instead.

自动播放:

  1. <video src="abc.mov" autoplay>
  2. </video>

使用poster在视频无法加载时显示图片:

  1. <video width="640" height="360" src="http://www.youtube.com/demo/google_main.mp" autobuffer controls poster="whale.png">
  2. <p>Try this page in Safari 4! Or you can <a href="http://www.youtube.com/demo/google_main.mp4">download the video</a> instead.</p>
  3. </video>

一个比较简洁的例子:

  1. <script type="text/javascript">
  2. function vidplay() {
  3. var video = document.getElementById("Video1");
  4. var button = document.getElementById("play");
  5. if (video.paused) {
  6. video.play();
  7. button.textContent = "||";
  8. } else {
  9. video.pause();
  10. button.textContent = ">";
  11. }
  12. }
  13. function restart() {
  14. var video = document.getElementById("Video1");
  15. video.currentTime = 0;
  16. }
  17. function skip(value) {
  18. var video = document.getElementById("Video1");
  19. video.currentTime += value;
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <video id="Video1" >
  25. // Replace these with your own video files.
  26. <source src="demo.mp4" type="video/mp4" />
  27. <source src="demo.ogv" type="video/ogg" />
  28. HTML5 Video is required for this example.
  29. <a href="demo.mp4">Download the video</a> file.
  30. </video>
  31. <p id="buttonbar">
  32. <button id="restart" onclick="restart();">[]</button>
  33. <button id="rew" onclick="skip(-10)"><<</button>
  34. <button id="play" onclick="vidplay()">></button>
  35. <button id="fastFwd" onclick="skip(10)">>></button>
  36. </p>

下面是一个比较完整的例子:

  1. <html >
  2. <head>
  3. <title>Full player example</title>
  4. <!-- Uncomment the following meta tag if you have issues rendering this page on an intranet or local site. -->
  5. <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"/> -->
  6. <script type="text/javascript">
  7. function init() { // Master function, encapsulates all functions
  8. var video = document.getElementById("Video1");
  9. if (video.canPlayType) { // tests that we have HTML5 video support
  10. // if successful, display buttons and set up events
  11. document.getElementById("buttonbar").style.display = "block";
  12. document.getElementById("inputField").style.display = "block";
  13. // helper functions
  14. // play video
  15. function vidplay(evt) {
  16. if (video.src == "") { // inital source load
  17. getVideo();
  18. }
  19. button = evt.target; // get the button id to swap the text based on the state
  20. if (video.paused) { // play the file, and display pause symbol
  21. video.play();
  22. button.textContent = "||";
  23. } else { // pause the file, and display play symbol
  24. video.pause();
  25. button.textContent = ">";
  26. }
  27. }
  28. // load video file from input field
  29. function getVideo() {
  30. var fileURL = document.getElementById("videoFile").value; // get input field
  31. if (fileURL != "") {
  32. video.src = fileURL;
  33. video.load(); // if HTML source element is used
  34. document.getElementById("play").click(); // start play
  35. } else {
  36. errMessage("Enter a valid video URL"); // fail silently
  37. }
  38. }
  39. // button helper functions
  40. // skip forward, backward, or restart
  41. function setTime(tValue) {
  42. // if no video is loaded, this throws an exception
  43. try {
  44. if (tValue == 0) {
  45. video.currentTime = tValue;
  46. }
  47. else {
  48. video.currentTime += tValue;
  49. }
  50. } catch (err) {
  51. // errMessage(err) // show exception
  52. errMessage("Video content might not be loaded");
  53. }
  54. }
  55. // display an error message
  56. function errMessage(msg) {
  57. // displays an error message for 5 seconds then clears it
  58. document.getElementById("errorMsg").textContent = msg;
  59. setTimeout("document.getElementById('errorMsg').textContent=''", 5000);
  60. }
  61. // change volume based on incoming value
  62. function setVol(value) {
  63. var vol = video.volume;
  64. vol += value;
  65. // test for range 0 - 1 to avoid exceptions
  66. if (vol >= 0 && vol <= 1) {
  67. // if valid value, use it
  68. video.volume = vol;
  69. } else {
  70. // otherwise substitute a 0 or 1
  71. video.volume = (vol < 0) ? 0 : 1;
  72. }
  73. }
  74. // button events
  75. // Play
  76. document.getElementById("play").addEventListener("click", vidplay, false);
  77. // Restart
  78. document.getElementById("restart").addEventListener("click", function () {
  79. setTime(0);
  80. }, false);
  81. // Skip backward 10 seconds
  82. document.getElementById("rew").addEventListener("click", function () {
  83. setTime(-10);
  84. }, false);
  85. // Skip forward 10 seconds
  86. document.getElementById("fwd").addEventListener("click", function () {
  87. setTime(10);
  88. }, false);
  89. // set src == latest video file URL
  90. document.getElementById("loadVideo").addEventListener("click", getVideo, false);
  91. // fail with message
  92. video.addEventListener("error", function (err) {
  93. errMessage(err);
  94. }, true);
  95. // volume buttons
  96. document.getElementById("volDn").addEventListener("click", function () {
  97. setVol(-.1); // down by 10%
  98. }, false);
  99. document.getElementById("volUp").addEventListener("click", function () {
  100. setVol(.1); // up by 10%
  101. }, false);
  102. // playback speed buttons
  103. document.getElementById("slower").addEventListener("click", function () {
  104. video.playbackRate -= .25;
  105. }, false);
  106. document.getElementById("faster").addEventListener("click", function () {
  107. video.playbackRate += .25;
  108. }, false);
  109. document.getElementById("normal").addEventListener("click", function () {
  110. video.playbackRate = 1;
  111. }, false);
  112. document.getElementById("mute").addEventListener("click", function (evt) {
  113. if (video.muted) {
  114. video.muted = false;
  115. evt.target.innerHTML = "<img alt='volume on button' src='vol2.png' />"
  116. } else {
  117. video.muted = true;
  118. evt.target.innerHTML = "<img alt='volume off button' src='mute2.png' />"
  119. }
  120. }, false);
  121. } // end of runtime
  122. }// end of master
  123. </script>
  124. </head>
  125. <body onload="init();" >
  126. <video id="Video1" controls style="border: 1px solid blue;" height="240" width="320" title="video element">
  127. HTML5 Video is required for this example
  128. </video>
  129. <p id="buttonbar" style="display: none;")>
  130. <button id="restart" title="Restart button">[]</button>
  131. <button id="slower" title="Slower playback button">-</button>
  132. <button id="rew" title="Rewind button" ><<</button>
  133. <button id="play" title="Play button">></button>
  134. <button id="fwd" title="Forward button" >>></button>
  135. <button id="faster" title="Faster playback button">+</button>
  136. <button id="Button2" title="Mute button" ><img alt="Volume on button" src="vol2.png" /></button>
  137. <br />
  138. <label>Playback </label>
  139. <label>Reset playback rate: </label><button id="normal" title="Reset playback rate button">=</button>
  140. <label> Volume </label>
  141. <button id="volDn" title="Volume down button">-</button>
  142. <button id="volUp" title="Volume up button">+</button>
  143. <button id="mute" title="Mute button" ><img alt="Volume on button" src="vol2.png" /></button>
  144. </p>
  145. <br/>
  146. <p id= "inputField" style="display:none;" >
  147. <label>Type or paste a video URL: <br/>
  148. <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" />
  149. <button id="loadVideo" title="Load video button" >Load</button>
  150. </label>
  151. </p>
  152. <p title="Error message area" id="errorMsg" style="color:Red;"></p>
  153. </body>
  154. </html>

人气教程排行