WeChat Open Platform

Mini Programs Documentation

Audio Component Controls

wx.createAudioContext(audioId)

Creates and returns audio context audioContext objects

audioContext

audioContext is bound to an <audio/> component using audioId. It can be used to operate the corresponding <audio/> component.

audioContext object method list:

Method Parameter Description
setSrc src Audio address
play No Play
pause No Pause
seek position Jumps to a specified position, unit s

Sample code:

<!-- audio.wxml -->
<audio  src="{{src}}" id="myAudio" ></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')
    this.audioCtx.setSrc('http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46')
    this.audioCtx.play()
  },
  data: {
    src: ''
  },
  audioPlay: function () {
    this.audioCtx.play()
  },
  audioPause: function () {
    this.audioCtx.pause()
  },
  audio14: function () {
    this.audioCtx.seek(14)
  },
  audioStart: function () {
    this.audioCtx.seek(0)
  }
})