微信小程序 条形加载读取进度 切换音频进度。简单实现(官方文档)

自定义实现音频播放,连接如下
https://blog.csdn.net/qq_35349982/article/details/102778340
这个是官方的音频
在这里插入图片描述

wxml文件 。主要是 slider标签的属性

<view class="page">
  <view class="page__hd">
    <text class="page__title">audio</text>
    <text class="page__desc">音频</text>
  </view>
  <view class="page__bd">
    <view class="section section_gap">
      <audio src="{{current.src}}" poster="{{current.poster}}" name="{{current.name}}" author="{{current.author}}" action="{{audioAction}}" bindplay="audioPlayed" bindtimeupdate="audioTimeUpdated" controls></audio>
    </view>
  </view>

  <view class="section section_gap">
    <text class="section__title">播放</text>
    <view class="body-view">
      <button bindtap="playAudio">播放</button>
    </view>
  </view>

  <view class="section section_gap">
    <text class="section__title">暂停</text>
    <view class="body-view">
      <button bindtap="pauseAudio">暂停</button>
    </view>
  </view>

  <view class="section section_gap">
    <text class="section__title">进度</text>
    <view class="body-view">
      <slider bindchange="timeSliderChanged"  bindchanging="Changed" value="{{sliderV}}" max="{{sliderMax}}" left-icon="cancel"  right-icon="success_no_circle"/>
    </view>
  </view>
</view>

js文件 。主要是滑动的监听函数。设置一个bool值

Page({
  data: {
    bo:true,
    sliderV:0,
    sliderMax: 414,
    current: {
      poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
      name: '此时此刻',
      author: '许巍',
      src: 'https://img.kuang100.net/daoyou/yinping/1568713339138.mp3',
    },
    audioAction: {
      method: 'pause'
    }
  },
  audioPlayed: function (e) {
    console.log('audio is played')
  },

  audioTimeUpdated: function (e) {
    if (this.data.bo) {
    console.log("正在播放")
    //设置滚动条
    this.setData({
      sliderV: e.detail.currentTime,
      bo: true
    });
    this.duration = e.detail.duration;
    }
  },

  timeSliderChanged: function (e) {
    console.log("拖动完成")
    if (!this.data.bo){
      this.setData({
        bo:true,
        audioAction: {
          method: 'setCurrentTime',
          data: e.detail.value
        }
      });
    }
  
  },

  Changed: function(e) {
    console.log("正在拖动")
    this.setData({
      bo: false
    });
  },




  playAudio: function () {
    this.setData({
      audioAction: {
        method: 'play'
      }
    });
  },
  pauseAudio: function () {
    this.setData({
      audioAction: {
        method: 'pause'
      }
    });
  },
})

代码仓库地址:https://gitee.com/gy297879328/applet_development.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值