温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

js如何实现音频控制进度条功能

发布时间:2021-06-21 11:36:49 阅读:227 作者:小新 栏目:web开发
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍js如何实现音频控制进度条功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

效果图:

js如何实现音频控制进度条功能

代码如下:

<!doctype html>
<html>
<head>
<meta name="author" content="dony">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>音频控制进度条</title>
<style type="text/css">

body,div{padding0;margin0;}
.m-main{width:560pxheight100%margin3% auto; background-color:#30a5ff;}
.m-main video{display: none; }
.m-main .player {
  width100%;
  height150px;
  position: relative;
  bottom0px;
}
.m-main .player>a{display: inline-block; width20%margin0 auto; padding5%color#FFFtext-align: center;}
.m-main .play-box {
  width100%;
  margin0 auto;
}

.m-main .play-box .left {
  width100%;
  float: left;
}

.m-main .play-box .left p.timeline { width70%;height10pxbackground-colorrgba(2162162160.5); border-radius5pxmargin:30px auto 0position: relative; z-index2; }
.m-main .play-box .left p.timeline span {position: relative; width0pxheight10pxbackground-color#FFFborder-radius5pxdisplay: block; -webkit-transition: width ease-out 0.3s;-o-transition: width ease-out 0.3s;transition: width ease-out 0.3s;}
.m-main .play-box .left p.timeline span:after{content""position: absolute; top: -4pxright:-0.6rem;width1.2remheight:1.2remborder-radius50%;background-color#FFF;}
.m-main .play-box .left div.info { height26pxline-height26pxfont-size14pxcolor#000position: relative; top: -18pxmargin:0 10pxz-index1;color#FFF;}
.m-main .play-box .left div.info .size { float: left; display: block;}
.m-main .play-box .left div.info .timeshow { float: right; display: block;}
</style>
</head>
<body>
<div class="m-main">
  <div class="player">
   <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" id="js-play">播放</a>
   <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" id="js-pause">暂停</a>
    <div class="play-box">
      <div class="left">
        <p class="timeline"><span ></span></p>
        <div class="info">
          <span class="size">00:00</span>
          <span class="timeshow">00:00</span>
        </div>
      </div>
    </div>
  </div>
 <div class="video">
 <video controls autoplay name="media" id="js-video"><source src="http://chipsguide.snaillove.com/Public/Uploads/file_server_storage/Audio/2017/03/23/19/192_20003137.mp3" type="video/mp4"></video>
 </div>
</div>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){
   AudioControl('js-video')
   function AudioControl(id){
     // 音频控制进度条
     var audio = document.getElementById(id);
     $(audio).on('loadedmetadata',function(){
    audio.pause();
    // 进度条控制
     $(document).on('touchend','.timeline',function(e){
       var x = e.originalEvent.changedTouches[0].clientX-this.offsetLeft;
       var X = x < 0 ? 0 : x ;
       var W = $(this).width();
       var place = X > W ? W : X;
       audio.currentTime = (place/W).toFixed(2)*audio.duration
       $(this).children().css({width:(place/W).toFixed(2)*100+"%"})
     });
     // 播放
    $(document).on('click','#js-play',function(){
    audio.play()
    });
    // 暂停
    $(document).on('click','#js-pause',function(){
    audio.pause()
    });
     })
     setInterval(function () {
       var currentTime = audio.currentTime;
       setTimeShow(currentTime);
     }, 1000);
     // 设置播放时间
   function setTimeShow(t) {
     t = Math.floor(t);
     var playTime = secondToMin(audio.currentTime);
     $(".size").html(playTime);
     $('.timeshow').text(secondToMin(audio.duration))
     $('.timeline').children().css({width:(t/audio.duration).toFixed(4)*100+"%"})
   }
     // 计算时间
   function secondToMin(s) {
     var MM = Math.floor(s / 60);
     var SS = s % 60;
     if (MM < 10)
       MM = "0" + MM;
     if (SS < 10)
       SS = "0" + SS;
     var min = MM + ":" + SS;
     return min.split('.')[0];
   }
   }
  })
</script>
</body>
</html>

以上是“js如何实现音频控制进度条功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI

开发者交流群×