微信小程序 audio 音频 组件

完整微信小程序(Java后端) 技术贴目录清单页面(必看)

音频。1.6.0版本开始,该组件不再维护。建议使用能力更强的 wx.createInnerAudioContext 接口

属性类型默认值必填说明最低版本
idstringaudio 组件的唯一标识符1.0.0
srcstring要播放音频的资源地址1.0.0
loopbooleanfalse是否循环播放1.0.0
controlsbooleanfalse是否显示默认控件1.0.0
posterstring默认控件上的音频封面的图片资源地址,如果 controls 属性值为 false 则设置 poster 无效1.0.0
namestring未知音频默认控件上的音频名字,如果 controls 属性值为 false 则设置 name 无效1.0.0
authorstring未知作者默认控件上的作者名字,如果 controls 属性值为 false 则设置 author 无效1.0.0
binderroreventhandle当发生错误时触发 error 事件,detail = {errMsg:MediaError.code}1.0.0
bindplayeventhandle当开始/继续播放时触发play事件1.0.0
bindpauseeventhandle当暂停播放时触发 pause 事件1.0.0
bindtimeupdateeventhandle当播放进度改变时触发 timeupdate 事件,detail = {currentTime, duration}1.0.0
bindendedeventhandle当播放到末尾时触发 ended 事件1.0.0

MediaError.code

返回错误码描述
1获取资源被用户禁止
2网络错误
3解码错误
4不合适资源

示例代码

在开发者工具中预览效果

index.xml

<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" left-icon="cancel" right-icon="success_no_circle"/>
    </view>
  </view>
  <view class="section section_gap">
    <text class="section__title">播放速率</text>
    <view class="body-view">
      <slider min="1" max="4" bindchange="playbackRateSliderChanged" left-icon="cancel" right-icon="success_no_circle"/>
    </view>
  </view>
</view>

index.js

Page({
  data: {
    current: {
      poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
      name: '此时此刻',
      author: '许巍',
      src: 'https://win-web-nf01-sycdn.kuwo.cn/dfec2ffc8e070e96101b21bd804de6e7/60bed777/resource/n1/73/40/4073043717.mp3',
    },
    audioAction: {
      method: 'pause'
    }
  },
  audioPlayed: function (e) {
    console.log('audio is played')
  },
  audioTimeUpdated: function (e) {
    this.duration = e.detail.duration;
  },

  timeSliderChanged: function (e) {
    if (!this.duration)
      return;

    var time = this.duration * e.detail.value / 100;

    this.setData({
      audioAction: {
        method: 'setCurrentTime',
        data: time
      }
    });
  },
  playbackRateSliderChanged: function (e) {
    this.setData({
      audioAction: {
        method: 'setPlaybackRate',
        data: e.detail.value
      }
    })
  },

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

运行效果:

在这里插入图片描述

微信搜一搜【java1234】关注这个放荡不羁的程序员,关注后回复【资料】有我准备的一线大厂笔试面试资料以及简历模板。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值