WeChat Open Platform

Mini Programs Documentation

audio

Audio

Attribute name Type Default value Description
id String audio component unique identifier
src String Resource address for audio that needs to be played
loop Boolean false Whether loop playback
controls Boolean true Whether default control displayed
poster String Audio cover image resource address on default control, if controls attribute value is false, then setting poster will be ineffective
name String Unknown audio Audio name on default control, if controls attribute value is false, then setting name will be ineffective
author String Unknown author Author name on default control, if controls attribute value is false, then setting author will be ineffective
binderror EventHandle error event triggered when an error occurs, detail = {errMsg: MediaError.code}
bindplay EventHandle play event triggered when starting/continuing playback
bindpause EventHandle pause event triggered when playback paused
bindtimeupdate EventHandle timeupdate event triggered when playback progress changes, detail = {currentTime, duration}
bindended EventHandle ended event triggered when playback ends

MediaError.code

Error return code Description
MEDIA_ERR_ABORTED Getting resources disabled by user
MEDIA_ERR_NETWORD Network error
MEDIA_ERR_DECODE Decode error
MEDIA_ERR_SRC_NOT_SUPPOERTED Inappropriate resource

Sample code:

<!-- audio.wxml -->
<audio poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" id="myAudio" controls loop></audio>

<button type="primary" bindtap="audioPlay">Play</button>
<button type="primary" bindtap="audioPause">Pause</button>
<button type="primary" bindtap="audio14">Set current play time as 14 seconds</button>
<button type="primary" bindtap="audioStart">Return to start</button>
// audio.js
Page({
  onReady: function (e) {
    // Use wx.createAudioContext to get audio context
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  data: {
    poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
    name: 'At The Moment',
    author: 'Xu Wei',
    src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
  },
  audioPlay: function () {
    this.audioCtx.play()
  },
  audioPause: function () {
    this.audioCtx.pause()
  },
  audio14: function () {
    this.audioCtx.seek(14)
  },
  audioStart: function () {
    this.audioCtx.seek(0)
  }
})

audio

Related api: wx.createAudioContext