Skip to main content
Version: 3.x

InnerAudioContext

The InnerAudioContext instance can be obtained through the API Taro.createInnerAudioContext.

Supported Formats

FormatiOSAndroid
flacx
m4a
oggx
apex
amrx
wmax
wav
mp3
mp4x
aac
aiffx
cafx

Reference

Methods

PropertyTypeDefaultRequiredDescription
autoplaybooleanfalseNoWhether to enable auto playback.
bufferednumberYesThe position where the audio is buffered. Only the part between the position where the playback has got to and this position is buffered (read only).
currentTimenumberYesThe position where the playback has got to (in sec). It is only returned when a valid src attribute exists and is rounded to six decimal places (read only).
durationnumberYesThe length of the audio file (in sec). It is only returned when a valid src attribute exists (read only).
loopbooleanfalseNoWhether to enable loop playback.
obeyMuteSwitchbooleantrueNoWhether to follow the "Mute" switch. If it is set to false, the audio file still sounds even if the "Mute" switch is on. As of base library 2.3.0, this parameter does not take effect and the feature is set through the API Taro.setInnerAudioOption.
pausedbooleanYesWhether the playback is paused or stopped (read only).
srcstringNoThe audio file address which can be used to play the audio file directly.
startTimenumber0NoThe position where the playback starts (in sec).
volumenumber1NoSpecifies the volume, which ranges from 0 to 1. It is 1 by default.

play

Plays an audio file.

() => void

pause

Pauses the audio playback. The playback is resumed at the point where it was paused.

() => void

stop

Stops the audio playback. The playback starts from the beginning if the file is played again.

() => void

seek

Jumps to the specific position.

(position: number) => void
PropertyType
positionnumber

destroy

Terminates the current instance.

() => void

onCanplay

Listens on the event that an audio file is ready for playback. A smooth playback is not guaranteed.

(callback?: () => void) => void
PropertyType
callback() => void

onPlay

Listens on the audio playback event.

(callback?: () => void) => void
PropertyType
callback() => void

onPause

Listens on the audio pause event.

(callback?: () => void) => void
PropertyType
callback() => void

onStop

Listens on the event of stopping audio playback.

(callback?: () => void) => void
PropertyType
callback() => void

onEnded

Listens on the event of playing an audio file to the end without interruption.

(callback?: () => void) => void
PropertyType
callback() => void

onTimeUpdate

Listens on the event of updating audio playback progress.

(callback?: () => void) => void
PropertyType
callback() => void

onError

Listens on the audio playback error event.

(callback?: (res: onErrorDetail) => void) => void
PropertyType
callback(res: onErrorDetail) => void

onWaiting

Listens on the audio loading event. It is triggered when the playback of an audio file stops to load the file due to insufficient data.

(callback?: () => void) => void
PropertyType
callback() => void

onSeeking

Listens on the event of jumping to a specific position in the audio file.

(callback?: () => void) => void
PropertyType
callback() => void

onSeeked

Listens on the event of finishing the jump to a specific position in the audio file.

(callback?: () => void) => void
PropertyType
callback() => void

offCanplay

Un-listens on the event that an audio file is ready for playback.

(callback?: () => void) => void
PropertyType
callback() => void

offPlay

Un-listens on the audio playback event.

(callback?: () => void) => void
PropertyType
callback() => void

offPause

Un-listens on the audio pause event.

(callback?: () => void) => void
PropertyType
callback() => void

offStop

Un-listens on the event of stopping audio playback.

(callback?: () => void) => void
PropertyType
callback() => void

offEnded

Un-listens on the event of playing an audio file to the end without interruption.

(callback?: () => void) => void
PropertyType
callback() => void

offTimeUpdate

Un-listens on the event of updating audio playback progress.

(callback?: () => void) => void
PropertyType
callback() => void

offError

Un-listens on the audio playback error event.

(callback?: () => void) => void
PropertyType
callback() => void

offWaiting

Un-listens on the audio loading event.

(callback?: () => void) => void
PropertyType
callback() => void

offSeeking

Un-listens on the event of jumping to a specific position in the audio file.

(callback?: () => void) => void
PropertyType
callback() => void

offSeeked

Un-listens on the event of finishing the jump to a specific position in the audio file.

(callback?: () => void) => void
PropertyType
callback() => void

Parameters

onErrorDetail

PropertyTypeDescription
errCodenumberError code
errMsgstringError message

onErrorDetailErrCode

Valid values of errCode

ValueDescription
10001System error
10002Network error
10003File error
10004Format error
-1Unknown error

Sample Code

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

API Support

APIWeChat Mini-ProgramH5React Native
InnerAudioContext.play✔️✔️✔️
InnerAudioContext.pause✔️✔️✔️
InnerAudioContext.stop✔️✔️✔️
InnerAudioContext.seek✔️✔️✔️
InnerAudioContext.destroy✔️✔️
InnerAudioContext.onCanplay✔️✔️
InnerAudioContext.onPlay✔️✔️
InnerAudioContext.onPause✔️✔️
InnerAudioContext.onStop✔️✔️
InnerAudioContext.onEnded✔️✔️
InnerAudioContext.onTimeUpdate✔️✔️
InnerAudioContext.onError✔️✔️
InnerAudioContext.onWaiting✔️✔️
InnerAudioContext.onSeeking✔️✔️
InnerAudioContext.onSeeked✔️✔️
InnerAudioContext.offCanplay✔️✔️
InnerAudioContext.offPlay✔️✔️
InnerAudioContext.offPause✔️✔️
InnerAudioContext.offStop✔️✔️
InnerAudioContext.offEnded✔️✔️
InnerAudioContext.offTimeUpdate✔️✔️
InnerAudioContext.offError✔️✔️
InnerAudioContext.offWaiting✔️✔️
InnerAudioContext.offSeeking✔️✔️
InnerAudioContext.offSeeked✔️✔️