html5媒体播放例子代码

╰半橙微兮° 2022-07-14 01:48 241阅读 0赞
  1. <!DOCTYPE html>
  2. <html>
  3. <meta charset=gb2312" />
  4. <div>
  5. <div>
  6. <video id="video" src="4_1.mp4" width="600">当前浏览器不支持video元素</video>
  7. </div>
  8. <div id="progressTime" style="display:none">
  9. <div style="float:left">
  10. <progress id="progress" max="100" style="width:450px">
  11. </progress>
  12. </div>
  13. <div id="showTime" style="float:left;margin-left:15px"></div>
  14. <div style="clear:both"></div>
  15. </div>
  16. </div>
  17. <div>
  18. <input type="button" id ="btnPlay" οnclick="playOrPause()" value="播放"/>
  19. <input type="button" id="btnSpeedUp" οnclick="speedUp()" value="快放" />
  20. <input type="button" id="btnSpeedUpDown" οnclick="speedDown()" value="慢放" />
  21. <input type="button" id="btnVolumeUp" οnclick="volumeUp()" value="提高音量" />
  22. <input type="button" id="btnVolumeDown" οnclick="volumeDown()" value="降低音量" />
  23. </div>
  24. </div>
  25. <script>
  26. var speed=1; //播放速度
  27. var volume=1; //播放音量
  28. var video=document.getElementById("video");
  29. var playBtn=document.getElementById("btnPlay");
  30. var btnSpeedUp=document.getElementById("btnSpeedUp");
  31. var btnSpeedUpDown=document.getElementById("btnSpeedUpDown");
  32. var btnVolumeUp=document.getElementById("btnVolumeUp");
  33. var btnVolumeDown=document.getElementById("btnVolumeDown");
  34. var showTime=document.getElementById("showTime");
  35. video.addEventListener('timeupdate',updateProgress,false); //为播放器添加时间改变监听事件
  36. var progress=document.getElementById("progress");
  37. progress.οnclick=progressOnClick; //为progress控件添加点击事件
  38. //播放或暂停
  39. function playOrPause()
  40. {
  41. var progressTime=document.getElementById("progressTime");
  42. progressTime.style.display=""; //显示播放进度条和时间
  43. if(video.paused) //如果当前播放状态为暂停,点击后开始播放
  44. {
  45. playBtn.value="暂停";
  46. video.play();
  47. video.playbackRate=speed;
  48. video.volume=volume;
  49. //启用控制工具条其他按钮
  50. btnSpeedUp.disabled="";
  51. btnSpeedUpDown.disabled="";
  52. btnVolumeUp.disabled="";
  53. btnVolumeDown.disabled="";
  54. }
  55. else //如果当前播放状态为播放,点击后暂停播放
  56. {
  57. playBtn.value="播放";
  58. video.pause();
  59. //禁用控制条其他按钮
  60. btnSpeedUp.disabled="disabled";
  61. btnSpeedUpDown.disabled="disabled";
  62. btnVolumeUp.disabled="disabled";
  63. btnVolumeDown.disabled="disabled";
  64. }
  65. }
  66. //提高播放速度
  67. function speedUp()
  68. {
  69. video.playbackRate+=1;
  70. speed=video.playbackRate;
  71. }
  72. //降低播放速度
  73. function speedDown()
  74. {
  75. video.playbackRate-=1;
  76. if(video.playbackRate<0)
  77. {
  78. video.playbackRate=0;
  79. }
  80. speed=video.playbackRate;
  81. }
  82. //增大音量
  83. function volumeUp()
  84. {
  85. if(video.volume<1)
  86. {
  87. video.volume+=0.1;
  88. if(video.volume>0)
  89. {
  90. video.muted=false;
  91. }
  92. }
  93. volume=video.volume;
  94. }
  95. //降低音量
  96. function volumeDown()
  97. {
  98. if(video.volume>0)
  99. {
  100. video.volume-=0.1;
  101. if(video.volume==0)
  102. {
  103. video.muted=true;
  104. }
  105. }
  106. volume=video.volume;
  107. }
  108. //播放进度条点击事件,点击后从点击位置开始播放
  109. function progressOnClick(event)
  110. {
  111. var progress=document.getElementById("progress");
  112. if(event.offsetX) //获取鼠标当前点击位置与当前位置相比存在偏移量
  113. {
  114. video.currentTime=video.duration*(event.offsetX/progress.clientWidth); //设定播放器新的播放位置
  115. }
  116. else
  117. {
  118. video.currentTime=video.duration*(event.clientX/progress.clientWidth);
  119. }
  120. }
  121. //更新进度条状态
  122. function updateProgress()
  123. {
  124. var currentPercent=Math.round(Math.floor(video.currentTime)/Math.floor(video.duration)*100,0);//计算当前播放时长与视频总时长之比
  125. var progress=document.getElementById("progress");
  126. progress.value=currentPercent;
  127. showTime.innerHTML=calculateTime(Math.floor(video.currentTime))+"/"+calculateTime(Math.floor(video.duration));//显示播放时间
  128. }
  129. //将当前传入的时间转换为hh:MM:ss的格式
  130. function calculateTime(time)
  131. {
  132. var h;
  133. var m;
  134. var s;
  135. h=String(parseInt(time/3600,10));
  136. if(h.length==1)
  137. {
  138. h='0'+h;
  139. }
  140. m=String(parseInt((time%3600)/60,10));
  141. if(m.length==1)
  142. {
  143. m='0'+m;
  144. }
  145. s=String(parseInt(time%60),10)
  146. if(s.length==1)
  147. {
  148. s='0'+s;
  149. }
  150. return h+":"+m+":"+s;
  151. }
  152. </script>
  153. </html>

效果:

Center

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读