微信小程序:音乐播放器开发(进度条可拖拽)

前言 

这篇文章上一版本是用audio组件开发的播放器,随后反应音频加载速度慢的问题

又用小程序内置的背景音乐播放的方法写了一遍,逻辑是一样的逻辑,希望对大家有所帮助!


<view class='audiosBox'>
    <view class="audioOpen" bindtap="listenerButtonPlay" wx:if="{{!isOpen}}">
      <image class='image2' src="../../images/play.png" />
    </view>
    <view class="audioOpen" bindtap="listenerButtonPause" wx:if="{{isOpen}}">
      <image src="../../images/pauses.png" />
    </view>
    <view class='slid'>
      <slider bindchange="sliderChange"  block-size="12px" step="2" value="{{offset}}" max="{{max}}" selected-color="#4c9dee" />
      <view>
        <text class='times'>{{starttime}}</text> <!-- 进度时长 -->
       
        <text class='times'>{{duration}}</text>   <!-- 总时长 -->
      </view>
    </view>
</view>

js部分注意了:bug ios 播放时必须加title 不然会报错导致音乐不播放

//index.js
//获取应用实例
const bgMusic = wx.getBackgroundAudioManager()
const app = getApp()

Page({
  data: {
    isOpen: false,//播放开关
    starttime: '00:00', //正在播放时长
    duration: '06:41',   //总时长
    src:"http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46"
  },
  // 播放
  listenerButtonPlay: function () {
    var that = this
    //bug ios 播放时必须加title 不然会报错导致音乐不播放
    bgMusic.title = '此时此刻'  
    bgMusic.epname = '此时此刻'
    bgMusic.src = that.data.src;
    bgMusic.onTimeUpdate(() => { 
      //bgMusic.duration总时长  bgMusic.currentTime当前进度
      console.log(bgMusic.currentTime)
      var duration = bgMusic.duration; 
      var offset = bgMusic.currentTime;  
      var currentTime = parseInt(bgMusic.currentTime);
      var min = "0" + parseInt(currentTime / 60);
      var max = parseInt(bgMusic.duration);
      var sec = currentTime % 60;
      if (sec < 10) {
        sec = "0" + sec;
      };
      var starttime = min + ':' + sec;   /*  00:00  */
      that.setData({
        offset: currentTime,
        starttime: starttime,
        max: max,
        changePlay: true
      })
    })
    //播放结束
    bgMusic.onEnded(() => {
      that.setData({
        starttime: '00:00',
        isOpen: false,
        offset: 0
      })
      console.log("音乐播放结束");
    })
    bgMusic.play();
    that.setData({
      isOpen: true,
    })
  },
  //暂停播放
  listenerButtonPause(){
     var that = this
    bgMusic.pause()
    that.setData({
      isOpen: false,
    })
  },
  listenerButtonStop(){
    var that = this
    bgMusic.stop()
  },
  // 进度条拖拽
  sliderChange(e) {
    var that = this
    var offset = parseInt(e.detail.value);
    bgMusic.play();
    bgMusic.seek(offset);
    that.setData({
      isOpen: true,
    })
  },
  // 页面卸载时停止播放
  onUnload() {
    var that = this
    that.listenerButtonStop()//停止播放
    console.log("离开")
  },
})

css部分 

/**index.wxss**/
.audiosBox{
  width: 92%;
  margin: auto;
  height: 130rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #f6f7f7;
  border-radius: 10rpx;
}
/*按钮大小  */
.audioOpen{
  width: 70rpx;
  height: 70rpx;
  border: 2rpx solid #4c9dee;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 20rpx;
}

image{
  width: 30rpx;
  height: 40rpx;
}
.image2{
  margin-left: 10%;
}
/*进度条长度  */
.slid{
  flex: 1;
  position: relative;
}
.slid view{
  display: flex;
  justify-content: space-between;
}
.slid view>text:nth-child(1){
  color: #4c9dee;
  margin-left:6rpx; 
}
.slid view>text:nth-child(2){
  margin-right:6rpx; 
}
slider{
  width: 520rpx;
  margin: 0;
  margin-left: 35rpx;
}
/*横向布局  */
.times{
  width: 100rpx;
  text-align: center;
  display: inline-block;
  font-size: 24rpx;
  color:#999999;
  margin-top: 5rpx;
}
.title view{
  text-indent: 2em;

}

看到这里,加个关注 点个赞吧!

 

 

喜欢上方小程序,需要源码的,添加博主微信私信小编.

  • 16
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论
要实现微信小程序video组件的进度条拖拽功能,可以使用以下步骤: 1. 在.wxml文件中,使用video组件,并加上一个view容器作为进度条的背景,再加上一个view作为进度条的滑块,代码如下: ```html <video src="{{src}}" poster="{{poster}}" bindtimeupdate="timeUpdate"></video> <view class="progress-bar"> <view class="progress" style="width: {{progress}}%;"></view> <view class="progress-btn" style="left: {{btnLeft}}px;"></view> </view> ``` 2. 在.wxss文件中,设置进度条和滑块的样式: ```css .progress-bar { position: relative; height: 6px; background-color: #ddd; } .progress { position: absolute; top: 0; left: 0; height: 6px; background-color: #f00; } .progress-btn { position: absolute; top: -5px; left: 0; width: 10px; height: 16px; border-radius: 50%; background-color: #fff; border: 2px solid #f00; box-shadow: 0 0 2px #f00; transform: translateX(-50%); } ``` 3. 在.js文件中,定义一个data对象,包含视频的总时长、当前播放时间、进度条长度和滑块位置等信息: ```javascript data: { src: 'http://www.example.com/video.mp4', poster: 'http://www.example.com/poster.jpg', duration: 0, currentTime: 0, progress: 0, btnLeft: 0, } ``` 4. 在onLoad生命周期函数中,获取视频的总时长: ```javascript onLoad: function() { var that = this; wx.createVideoContext('myVideo', that).requestFullScreen(); wx.createVideoContext('myVideo', that).play(); wx.createVideoContext('myVideo', that).pause(); wx.createVideoContext('myVideo', that).seek(0); wx.createVideoContext('myVideo', that).play(); wx.createVideoContext('myVideo', that).on('loadedmetadata', function(res) { that.setData({ duration: res.duration }); }); }, ``` 5. 在timeUpdate事件回调函数中,更新当前播放时间和进度条长度以及滑块位置: ```javascript timeUpdate: function(e) { var that = this; var currentTime = e.detail.currentTime; var duration = that.data.duration; var progress = Math.floor(currentTime / duration * 100); var btnLeft = progress * 3.4 - 7; that.setData({ currentTime: currentTime, progress: progress, btnLeft: btnLeft }); }, ``` 6. 在进度条的view容器上,绑定touchstart、touchmove和touchend事件,实现滑动改变进度条长度和滑块位置: ```javascript touchstart: function(e) { var that = this; that.setData({ startX: e.touches[0].pageX, }); }, touchmove: function(e) { var that = this; var startX = that.data.startX; var moveX = e.touches[0].pageX; var distance = moveX - startX; var duration = that.data.duration; var progress = that.data.progress + distance / 340 * 100; if (progress < 0) { progress = 0; } else if (progress > 100) { progress = 100; } var currentTime = progress / 100 * duration; var btnLeft = progress * 3.4 - 7; that.setData({ progress: progress, currentTime: currentTime, btnLeft: btnLeft, startX: moveX, }); }, touchend: function(e) { var that = this; wx.createVideoContext('myVideo', that).seek(that.data.currentTime); }, ``` 通过以上步骤,就可以实现微信小程序video组件的进度条拖拽功能。
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端_李嘉豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值