微信小程序音频播放

微信小程序音频播放

先看效果
在这里插入图片描述
代码为

<!-- 音频 -->
<view>
  <view class="cu-bar search bg-white">
    <view class="cu-avatar round">
      <!-- cuIcon-stop -->
      <text class="cuIcon-playfill lg" bindtap="play" wx:if="{{plays}}"></text>
      <text class="cuIcon-stop lg" bindtap="outplay" wx:if="{{!plays}}"></text>
    </view>
    <view class="search-form radius" style="background-color: transparent;">
      <view class="cu-progress round xs" >
        <view class="bg-red" style="width:{{speed?speed:'0%'}};"></view>
      </view>
    </view>
    <view class="action">
      <text>{{time?time:'0:00'}}</text>
    </view>
  </view>
</view>

以上为前端代码

//在page上面添加一个获取音频容器的
let audioplay = wx.createInnerAudioContext(); //创建音频实例
Page({
	//播放函数
  play() {
    let that = this;
    // 资源来源:https://blog.csdn.net/u011025329/article/details/82082985
    audioplay.src = "cloud://cloud1-7gg85fhe4fd7b213.636c-cloud1-7gg85fhe4fd7b213-1305500809/cloudbase-cms/upload/2021-04-07/1moh7tvb02da40rxcwldumupcafcc0t2_.mp3";
    audioplay.play();
    setTimeout(() => {
      audioplay.currentTime;
      audioplay.onTimeUpdate(() => {
        let time =parseInt(audioplay.duration/60)+':'+parseInt(audioplay.duration%60);
        let speed = (audioplay.currentTime/audioplay.duration*100).toFixed(2)+'%';
          that.setData({
            time,
            speed
          })
            console.log(time)   //总时长
            console.log(speed)  //当前播放进度
        })
    }, 500)
    this.setData({plays:false})
  },
  //暂停函数
  outplay(){
    audioplay.pause();
    this.setData({plays:true})
  },
})

css样式是其他组件而来的

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值