Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat Small Program API Audio InnerAudioContext object


May 18, 2021 WeChat Mini Program Development Document


Table of contents


InnerAudioContext

InnerAudioContext instance, which can be obtained through the wx.createInnerAudioContext interface.



Property


string src

The address of the audio resource for direct playback. 2.2.3 Start supporting cloud file IDs


number startTime

Where playback starts (in:s), which defaults to 0


boolean autoplay

Whether to start playback automatically, the default is false


boolean loop

Whether to loop, the default is false


boolean obeyMuteSwitch

Whether to follow the system mute switch, the default is true. W hen this parameter is false, the sound continues even if the user turnes on the mute switch. This parameter does not take effect from version 2.3.0 and is set uniformly using the wx.setInnerAudioOption interface.


number volume

Volume. R ange 0 to 1. The default is 1


number playbackRate

Play speed. R ange 0.5-2.0, default to 1. (Android requires 6 and above versions)


number duration

The length of the current audio in s. Return only if there is a legitimate src (read-only)


number currentTime

The current audio playback position in s. Returned only if there is a legitimate src at the present time, 6 bits after the time is reserved for the number of points (read only)


boolean paused

Whether to pause or stop state (read-only)


number buffered

Audio buffering point in time, only to ensure that the content is buffered (read-only) from the current playback point in time to this point in time



Method:

InnerAudioContext.destroy()

Destroy the current instance


InnerAudioContext.offCanplay(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancels the event that listens for audio into a play-playing state

Parameters

function callback

The callback function for an event in which audio enters a playable state



InnerAudioContext.offEnded(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel the event that listens to audio that naturally plays to the end

Parameters

function callback

The callback function of the audio that naturally plays to the end event


InnerAudioContext.offError(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel listening for audio playback error events

Parameters

function callback

The callback function for the audio playback error event


InnerAudioContext.offPause(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel the listening audio pause event

Parameters

function callback

The callback function for the audio pause event


InnerAudioContext.offPlay(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel listening for audio playback events

Parameters

function callback

The callback function for the audio playback event


InnerAudioContext.offSeeked(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel the event that the audio is listening to complete the jump operation

Parameters

function callback

The callback function for the event in which the audio completes the jump operation


InnerAudioContext.offSeeking(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

The event that cancels the jump operation by listening to audio

Parameters

function callback

A callback function for events in which audio jumps


InnerAudioContext.offStop(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel listening for audio stop events

Parameters

function callback

The callback function for the audio stop event


InnerAudioContext.offTimeUpdate(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel listening to audio playback progress update events

Parameters

function callback

The callback function for the audio playback progress update event


InnerAudioContext.offWaiting(function callback)

Base library 1.9.0 starts to support, and low versions need to be compatible.

Cancel listening for events in the audio load

Parameters

function callback

The callback function for events in the audio load


InnerAudioContext.onCanplay(function callback)

Listen for events where audio enters a play-playing state. However, there is no guarantee that the back will play smoothly

Parameters

function callback

The callback function for an event in which audio enters a playable state


InnerAudioContext.onEnded(function callback)

Listen for events in which audio plays naturally to the end

Parameters

function callback

The callback function of the audio that naturally plays to the end event


InnerAudioContext.onError(function callback)

Listen for audio playback error events

Parameters

function callback

The callback function for the audio playback error event

Parameters

Object res
Property Type Description
errMsg string
errCode number

The legal value of errCode

Value Description The lowest version
10001 System error
10002 Network error
10003 The file is wrong
10004 The format is wrong
-1 Unknown error


InnerAudioContext.onPause(function callback)

Listen for audio pause events

Parameters

function callback

The callback function for the audio pause event


InnerAudioContext.onPlay(function callback)

Listen for audio playback events

Parameters

function callback

The callback function for the audio playback event


InnerAudioContext.onSeeked(function callback)

Listen for events in which audio completes a jump operation

Parameters

function callback

The callback function for the event in which the audio completes the jump operation


InnerAudioContext.onSeeking(function callback)

Listen for events in which audio jumps

Parameters

function callback

A callback function for events in which audio jumps


InnerAudioContext.onStop(function callback)

Listen for audio stop events

Parameters

function callback

The callback function for the audio stop event


InnerAudioContext.onTimeUpdate(function callback)

Listen for audio playback progress update events

Parameters

function callback

The callback function for the audio playback progress update event


InnerAudioContext.onWaiting(function callback)

Listen for events in the audio load. It fires when audio needs to stop loading because there is not enough data

Parameters

function callback

The callback function for events in the audio load


InnerAudioContext.pause()

Time out. Paused audio playback starts at the pause



InnerAudioContext.play()

Play


InnerAudioContext.seek(number position)

Jump to the specified location

Parameters

number position

The time of the jump, in s. 3 bits after the exact number of points, which supports ms-level accuracy


InnerAudioContext.stop()

Stop it. Audio playback after stopping plays from scratch.



The format is supported

Format Ios Android
Flac
m4a
ogg
ape
Amr
Wma
Wav
mp3
mp4
Aac
aiff
Caf

Example code:

const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.autoplay = true
innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
innerAudioContext.onPlay(() => {
  console.log('开始播放')
})
innerAudioContext.onError((res) => {
  console.log(res.errMsg)
  console.log(res.errCode)
})