微信小程序音频播放

微信小程序音频播放

先看效果
微信小程序音频播放
代码为

<!-- 音频 -->
<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样式是其他组件而来的

上一篇:匀速运动反向返回


下一篇:14周