可拖拽播放、可点击播放的音乐播放器(Vue)

<template>
   <div class='pro' @mouseup="mouseup" @mousemove="mousemove">
       <div class='p-b'>
        <div>
            <a-icon type="step-backward" />
        </div>
        <div v-if='pause' @click="play" class='p-f'>
            <a-icon type="pause" />
        </div>
        <div v-else @click="play" class='p-f'>
            <a-icon type="caret-right" />
        </div>
        <div>
            <a-icon type="step-forward" />
        </div>
       </div>
        <audio ref='audio' :src="url" @timeupdate="updateTime" @canplay="getDuration"></audio>
       <div class='p-p'>
           <span >{{this.currentTime?this.currentTime:'00:00'}}</span>
           <div @mousedown.self='clickChange'>
               <!-- 进度条 -->
               <!--  @mousedown.self='clickChange' -->
               <div ref='progress' @mousedown.self='clickChange'></div>
               <!-- 进度条上圆圈 -->
               <div ref='progressBar' @mousedown="mousedown" >
                   <div></div>
               </div>
           </div>
           <!-- {{$refs.audio.duration}} -->
           <span v-if='this.duration'>{{this.duration}}</span>
       </div>

       <div class='p-v'>
           <span><a-icon type="sound" /></span>
            <div class='p-c'>
               <div></div>
               <div class='b'>
                   <div></div>
               </div>
           </div>
       </div>

       <div class='p-s'>
           <img src="./imgs/1.png" alt="" width="180px" style='margin-top:-4px'>
       </div>
   </div>
</template>

<script>
import axios from 'axios';

export default {
  name:'',
  data(){
    return{
      pause:false,
      song:'',
      currentTime:'00:00',
      duration:0,
      url:'',
      id:'',
      progress:'',
      progressBar:'',
      x:'',
      dx:'',
      isMove:false,
      left:'',
      width:'',
      progressWidth:'',
      currentTimeStamp:0,
    }
  },
  methods:{
      play()
      {
        
          this.pause=!this.pause

          if(this.song.paused)
          {
              this.song.play();
          }else{
              this.song.pause();
          }
      },
      //该回调中获取duration   
      getDuration(e) {
      
        this.duration = this._changeForm(this.song.duration);

       },
      //播放过程中调用的回调
      updateTime(e) {
        this.currentTime = this._changeForm(e.target.currentTime);  //获取audio当前播放时间
        // console.log(this.song.volume=1);

        // 改变播放进度条长度
        this.progress.style.width=(e.target.currentTime/this.song.duration)*400+'px';

        // 改变播放进度条上圆圈位置
        this.progressBar.style.left=((e.target.currentTime/this.song.duration)*400-7.5)+'px';
        
        //在播放过程中要实时更新currentTimeStamp,否则点击抬起的事件中currentTimeStamp是拖拽/点击后的值,再次点击圆圈会回到该值
        this.currentTimeStamp=e.target.currentTime;

      },
      //改变播放时间格式
      _changeForm(time)
      {
          let min=Math.floor(time/60);
          let seconds=Math.floor(time%60);

          if(min<10)
          {
              min='0'+min;
          }
          if(seconds<10)
          {
              seconds='0'+seconds;
          }
          let timeStr=min+':'+seconds;
          return timeStr;
      },
      //拖拽改变播放进度
      mousedown(e)
      {
          this.x=e.clientX;
          
          if(this.progressBar.style.left=='')
          {
            this.left=0;
          }else{
            this.left=this.delPx(this.progressBar.style.left);
           
          }
          this.isMove=true;
          this.progressWidth=this.delPx(this.progress.style.width);
         
      },
      mousemove(e)
      {
          if(this.isMove)
          {
            this.dx=e.clientX;
            //圆圈移动的距离=自身位移加改变的距离
            let move=this.left*1+(this.dx-this.x);
            move=move<=-7.5? -7.5:move;
            move=move>=392.5? 392.5:move;

            //进度条随移动距离而改变
            let width=this.progressWidth*1+(this.dx-this.x);
            width=width<=0? 0:width;
            width=width>=400? 400:width;

            //视图呈现
            this.progressBar.style.left=move+'px';
            this.progress.style.width=width+'px';


            //拖动时,改变时间,不改变歌曲当前播放内容,松开时,改变歌曲播放内容
            this.currentTime=this._changeForm((width/400)*this.song.duration);
            this.currentTimeStamp=(width/400)*this.song.duration;

          }

      },
      mouseup(e)
      {
          if(this.isMove)  //只有点击进度条,松开后才会响应
          {
            this.isMove=false;
            this.song.currentTime=this.currentTimeStamp.toFixed(2);
            console.log('触发');
          }

      },
      delPx(str)
      {
          return str.substring(0,str.length-2)
      },
      //点击改变播放,当点击按钮时,e变成按钮对象,此时获取的坐标是在按钮内的坐标
      //解决方案:将点击事件分散到父元素和其他元素上,并添加.self修饰符,使得圆圈点击无效
      clickChange(e)
      {
          //e.offsetX获取相对于当前元素的位移坐标,设置进度条和圆圈的位置
          let width=e.offsetX;
          let left=e.offsetX-7.5;

          this.progress.style.width=width+'px';
          this.progressBar.style.left=left+'px';

          //改变播放内容
          this.song.currentTime=(width/400)*this.song.duration.toFixed(2);
          //因为还会触发鼠标抬起事件,而点击圆圈并不会触发拖动事件,会使得currentTimeStamp的值为上一次拖动结束的值
          //currentTimeStamp会改变currentTime,从而使得进度条位置为上一次拖动位置
          this.currentTimeStamp=(width/400)*this.song.duration.toFixed(2);

      }

  },
  async mounted()
  {
      
      let res=await axios.get('https://api.imjad.cn/cloudmusic/?type=song&id=254485');
      
      //设置歌曲id、播放地址、ref对象、进度条和播放进度条上圆圈
      this.id=res.data.data[0].id;
      this.url=res.data.data[0].url;
      this.song=this.$refs.audio;    
      this.progress=this.$refs.progress;
      this.progressBar=this.$refs.progressBar;
  },
  computed:{

  },
  watch:{

  }

}
</script>

<style scoped lang='less'>
.pro{
    background-color: #F6F6F8;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding:0 20px;
    border: solid 1px #ccc;
    .p-b{
        display: flex;
        align-items: center;
        flex:1;
        >div{
            margin: 0 15px;
            cursor: pointer;
        }
        .p-f{
            height: 35px;
            width: 35px;
            border-radius: 35px;
            text-align: center;
            line-height: 35px;
            background-color:#E83C3C ;
            color:white;
            font-size: 19px;
        }

        >div:first-child{
            height: 30px;
            width: 30px;
            border-radius: 30px;
            text-align: center;
            line-height: 30px;
            background-color:#E83C3C ;
            color:white;
        }
        >div:last-child{
            height: 30px;
            width: 30px;
            border-radius: 30px;
            text-align: center;
            line-height: 30px;
            background-color:#E83C3C ;
            color:white;
        }
    }
    .p-p{
        flex:3;
        display:flex;
        align-items: center;
        margin-left: 5px;
        >span{
            font-size: 12px;
            color:black;
            margin: 0 10px;
            -webkit-user-select:none;
            -moz-user-select:none;
            -ms-user-select:none;
            user-select:none;
        }
        >div{
            width: 400px;
            height: 5px;
            background-color: #C2C2C4;
            border-radius: 5px;
            position: relative;
            -webkit-user-select:none;
            -moz-user-select:none;
            -ms-user-select:none;
            user-select:none;
            >div:nth-child(1){
                position: absolute;
                left: 0;
                height: 5px;
                width: 0px;
                border-radius: 5px;
                background-color: #E83C3C;
            }
            >div:nth-child(2){
                position: absolute;
                cursor: pointer;
                left: -7.5px;
                top:-5px;
                height: 15px;
                width: 15px;
                border-radius: 10px;
                border: solid 1px #ccc;
                background-color: white;
                display: flex;
                align-items: center;
                >div{
                    height: 5px;
                    width: 5px;
                    border-radius: 5px;
                    background-color:#E83C3C ;
                    margin: 0 auto;
                }
            }
            >div:nth-child(2):hover{
                box-shadow: #E2E2E4 0.5px 0.5px 1px 1px,#E2E2E4 -0.5px -0.5px 1px 1px
            }
        }

    }
    .p-v{
        flex:1;
        display: flex;
        align-items: center;
        >span{
            margin-left: 10px;
            margin-right: 5px;
        }
        >.p-c{
        width: 100px;
        height: 3px;
        background-color: #C2C2C4;
        border-radius: 5px;
        position: relative;
        // cursor: pointer;
        >div:nth-child(1){
            position: absolute;
            left: 0;
            height: 3px;
            width: 50px;
            border-radius: 5px;
            background-color: #E83C3C;
        }

        >div:nth-child(2){
            position: absolute;
            cursor: pointer;
            left: 42.5px;
            top:-5px;
            height: 15px;
            width: 15px;
            border-radius: 10px;
            border: solid 1px #ccc;
            background-color: white;
            display: flex;
            align-items: center;
            display: none;
            >div{
                
                height: 5px;
                width: 5px;
                border-radius: 5px;
                background-color:#E83C3C ;
                margin: 0 auto;
            }
        }
    }
    }
    .p-s{
        flex:1;
        cursor: pointer;
    }
}
</style>

效果图:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
任意拖动的视频播放器 //====================================================== //===============VIDEO SETUP=====视频设置============ //====================================================== //the playlist address _root.playlist == undefined ? playlist="http://yun.365.sh/s/TY2gc2.swf" : playlist=_root.playlist; // stage variables Stage.showMenu = false; Stage.scaleMode = "noScale"; stop(); // video Stream variables var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); // buffer video ns.setBufferTime(5); ns.onStatus = function(obj) { if (obj.code == "NetStream.Buffer.Full") { bufferClip._visible = false; } if(obj.code == "NetStream.Buffer.Empty") { bufferClip._visible = true; } }; var myVolume:Number = 0; var current_song:Number = 1; // control variables var scr = _root.main.controlBar.scrubber; var pr = scr.progres; var ld = scr.loader; var cb = _root.main.controlBar; var swidth = scr.back._width-37; //297-37 var pstart = -285; //-261 var dur:Number; // the percentage of the video loaded var lprog:Number = 0; var sign:String = "first"; main.theVideo.attachVideo(ns); // parsing the playlist xml files data_xml = new XML(); data_xml.ignoreWhite = true; data_xml.onLoad = loadData; data_xml.load(playlist); var aPath = new Array(); function loadData(success) { if (success) { // getting all titles and filenames songTitel = new Array(); audioTracks = new Array(); audioTracks.shuffle(); audioTracks = this.firstChild.childNodes; song_total = audioTracks.length; for (var i = 0; i<song_total; i++) { aPath.push(audioTracks[i].attributes.path); songTitel.push(audioTracks[i].attributes.title); // buiding playlist buttons bot.listContentMc.playlist.btn.duplicateMovieClip("btn"+i, i); bot.listContentMc.playlist["btn"+i]._y = bot.listContentMc.playlist.btn._y+i*int(bot.listContentMc.playlist.btn._height)+i; bot.listContentMc.playlist["btn"+i].txt = checkDigits(i+1)+". "+songTitel[i]; bot.listContentMc.playlist["btn"+i].hit.onPress = function() { listClick(this._parent.getDepth()+1); }; } //checking autostart mode autoStart = this.firstChild.attributes.autoStart; if (autoStart == "yes") { playtime(); } else if (autoStart == "no") { main.controlBar.scrubber.perload.text = "OK"; cb.plabel.play(); } else if (autoStart == "random") { current_song = random(song_total)+1; playtime(); } else { current_song = int(this.firstChild.attributes.autoStart); playtime(); } } // done ! all loaded succesfully. purging trash delete audioTracks; delete data_xml; } //========================================================== //===============VIDEO CONTROLS=====视频控件================= //========================================================== function playtime() { bufferClip._visible = false; AudioPath = aPath[current_song-1]; main.welcome._visible = false; ns.play(AudioPath); cb.plabel.gotoAndStop(1); } // get the length of the Movie ns.onMetaData = function(obj) { dur = obj.duration; }; _root.onEnterFrame = videoUpdate; // the state of the video loading... function videoUpdate() { var prog = ns.time/dur; main.welcome.title.text = "<< " + songTitel[current_song-1] + ">> is preparing . "; lprog = ns.bytesLoaded/ns.bytesTotal; main.controlBar.scrubber.loaded._width = lprog*290; pr._x = -285+(swidth*prog); ns.onStatus = function(infoObject:Object) { if(infoObject.code == "NetStream.Play.Start") { sign = "started"; } else if(infoObject.code == "NetStream.Play.Stop") { sign = "closed"; if (autoStart == "random") { current_song = random(song_total)+1; } else { current_song == song_total ? current_song=1 : current_song++; } cb.plabel.play(); } }; if(sign == "started") { main.welcome._visible = false; bot.currentPlay.text = checkDigits(current_song)+". "+songTitel[current_song-1]; main.controlBar.scrubber.perload.text = Math.round(lprog*100)+"%"; hadPlayed.text = Math.floor(ns.time/60)+" m "+Math.round(ns.time%60)+" s "+Math.floor(prog*100)+" %"; timetext.text = Math.floor(dur/60)+" m "+Math.round(dur%60)+" s"; } else if(sign == "closed") { main.controlBar.scrubber.perload.text = "Closed"; hadPlayed.text = "Previous one closed"; main.welcome._visible = true; timetext.text = "none loaded"; } else if(sign == "first") { main.controlBar.scrubber.perload.text = "OK"; timetext.text = "默认的视频";//default Vedio OK hadPlayed.text = "玩家可以"; //Player OK } } function listClick(prm) { delete pausepos; current_song = prm; ns.stop(); playtime(); sign = "select"; } // list scroller bot.listContentMc.list_bg.onEnterFrame = function() { if (this.hitTest(_root._xmouse, _root._ymouse) == true && this._parent.playlist._height>this._height) { ymin = this._y+this._height-this._parent.playlist._height; ymax = this._y+3; conv = (this._ymouse-15)*1.3/this._height; conv>1 ? conv=1 : null; conv<0 ? conv=0 : null; this._parent.playlist.easeY(ymax-conv*(ymax-ymin)); } }; bot.listContentMc.playlist.setMask(bot.listContentMc.list_bg); MovieClip.prototype.easeY = function(t) { this.onEnterFrame = function() { this._y = int(t-(t-this._y)/1.5); if (this._y>t-1 && this._y<t+1) { delete this.onEnterFrame; } }; }; // prefixing a 0 to the time function checkDigits(toCheck) { return (toCheck<10) ? toCheck="0"+toCheck : toCheck; } // end playlist cb.replayer.onRollOver = function() { showTip("Replay Video"); }; cb.replayer.onRollOut = cb.replayer.onPress=function () { removeTip(); }; cb.replayer.onRelease = function() { ns.seek(0); ns.pause(false); cb.plabel.gotoAndStop(1); }; var rewInt; cb.rewinder.onRollOver = function() { showTip("Rewind"); }; cb.rewinder.onRollOut = function() { removeTip(); }; cb.rewinder.onPress = function() { rewInt = setInterval(rewind, 200); ns.pause(true); removeTip(); }; cb.rewinder.onRelease = cb.rewinder.onReleaseOutside=function () { clearInterval(rewInt); ns.pause(false); }; function rewind() { ns.seek(ns.time-30); if (ns.time == 0) { clearInterval(rewInt); ns.pause(false); } // end if } cb.player.onRollOver = function() { showTip("Play / Pause"); }; cb.player.onRollOut = cb.player.onPress=function () { removeTip(); }; cb.player.onRelease = function() { if(sign == "closed" || sign == "first" || sign == "select") { playtime(); } else ns.pause(); cb.plabel.play(); }; cb.stopper.onRollOver = function() { showTip("Stop Video"); }; cb.stopper.onRollOut = cb.stopper.onPress=function () { removeTip(); }; cb.stopper.onRelease = function() { ns.seek(0); ns.pause(true); cb.plabel.gotoAndStop(2); removeTip(); }; var ffInt; cb.fforwarder.onRollOver = function() { showTip("Fast Forward"); }; cb.fforwarder.onRollOut = function() { removeTip(); }; cb.fforwarder.onPress = function() { removeTip(); ffInt = setInterval(ff, 200); }; cb.fforwarder.onRelease = cb.fforwarder.onReleaseOutside=function () { clearInterval(ffInt); /*if(ns.time / dur > .95) { ns.seek(0); ns.pause(false); }*/ }; function ff() { if (ns.time/dur<.95) { ns.seek(ns.time+1); } else { clearInterval(ffInt); } } pr.scrubba.onPress = function() { //ns.pause(true); _root.onEnterFrame = scrubba; pr.startDrag(false, -285, pr._y, -25, pr._y); }; pr.scrubba.onRelease = pr.scrubba.onReleaseOutside=function () { _root.onEnterFrame = videoUpdate; //ns.pause(false); pr.stopDrag(); removeTip(); }; function scrubba() { var p = (pr._x-pstart)/swidth; if (p<=lprog) { ns.seek(p*dur); } else { ns.seek(lprog); } main.tooltip.datext.text = Math.floor(p*100)+" % 处理";//processing main.tooltip._x = Math.round(main._xmouse); main.tooltip._y = Math.round(main._ymouse); main.tooltip._visible = true; } main.controlBar.scrubber.back.onRelease = controlBar.scrubber.back.onReleaseOutside=function () { var p = (this._xmouse-37)/swidth; if (p<=lprog) { ns.seek(p*dur); } else { ns.seek(lprog); } }; //====================================================== //==============SOUND CONTROL=======声音控制============= //====================================================== _root.createEmptyMovieClip("vidsound", _root.getNextHighestDepth()); vidsound.attachAudio(ns); var sou:Sound = new Sound(vidsound); sou.setVolume(75); var startxs = main.controlBar.vol._x; main.controlBar.vol._x = startxs+(70*.75); main.controlBar.vol.onPress = function() { this.startDrag(false, startxs+3, this._y, startxs+58, this._y); main.tooltip._x = Math.round(main._xmouse); main.tooltip._y = 554; this.onEnterFrame = voller; }; main.controlBar.vol.onRollOver = function() { showTip("Volume"); }; main.controlBar.vol.onRollOut = function() { removeTip(); }; main.controlBar.vol.onRelease = main.controlBar.vol.onReleaseOutside = function () { this.stopDrag(); removeTip(); delete this.onEnterFrame; }; function voller() { var perc = ((main.controlBar.vol._x-544)/(55)); sou.setVolume(Math.ceil(perc*100)); myVolume = sou.getVolume(); main.tooltip.datext.text = sou.getVolume()+"% Volume"; main.tooltip._x = Math.round(main._xmouse); main.tooltip._y = Math.round(main._ymouse); main.tooltip._visible = true; } //====================================================== //==============TOOL TIPS=======工具提示================== //====================================================== var ti:Number; function showTip(datext) { ti = getTimer(); main.tooltip.datext.text = datext; main.tooltip.onEnterFrame = function() { var tempti = getTimer(); if (tempti-ti>500) { this._x = Math.round(main._xmouse); this._y = Math.round(main._ymouse); main.tooltip._visible = true; } }; } function removeTip() { main.tooltip._visible = false; delete main.tooltip.onEnterFrame; } main.tooltip._visible = false; main.mute.onRelease = function() { if (sou.getVolume() == 0) { sou.setVolume(myVolume); cb.vol._x = 544+63*myVolume/100; } else { myVolume = sou.getVolume(); sou.setVolume(0); cb.vol._x = 544; } main.bugle.play(); }; main.mute.onRollOver = function() { showTip("切换静音");//Toggle Mute }; main.mute.onRollOut = function() { removeTip(); }; //dragging main.window.onPress = function() { this._parent.startDrag(); this._parent.swapDepths(5); bot.swapDepths(4); }; bot.listTitleBar.onPress = bot.listContentMc.listContent.onPress =function() { bot.startDrag(); bot.swapDepths(5); main.swapDepths(4); }; main.window.onRelease = main.playerTitleBar.onReleaseOutside = function() { this._parent.stopDrag(); }; bot.listTitleBar.onRelease = bot.listTitleBar.onReleaseOutside = bot.listContentMc.listContent.onRelease = bot.listContentMc.listContent.onReleaseOutside = function() { bot.stopDrag(); }; // hide or show the playList main.plBut.onRelease = function() { (bot._visible == true)?bot._visible = false:bot._visible = true; }; main.plBut.onRollOver = function() { showTip("播放列表");//PlayList }; main.plBut.onRollOut = function() { removeTip(); }; bot.close.onRelease = function() { bot._visible = false; } bot.small.onRelease = function() { bot.smallContent.play(); (bot.listContentMc._visible == true)?bot.listContentMc._visible = false:bot.listContentMc._visible = true; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值