InnerAudioContext
The InnerAudioContext instance can be obtained through the API Taro.createInnerAudioContext.
Supported Formats
| Format | iOS | Android |
|---|---|---|
| flac | x | √ |
| m4a | √ | √ |
| ogg | x | √ |
| ape | x | √ |
| amr | x | √ |
| wma | x | √ |
| wav | √ | √ |
| mp3 | √ | √ |
| mp4 | x | √ |
| aac | √ | √ |
| aiff | √ | x |
| caf | √ | x |
Methods
| Property | Type | Default | Required | Description |
|---|---|---|---|---|
| autoplay | boolean | false | No | Whether to enable auto playback. |
| buffered | number | Yes | The 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). | |
| currentTime | number | Yes | The 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). | |
| duration | number | Yes | The length of the audio file (in sec). It is only returned when a valid src attribute exists (read only). | |
| loop | boolean | false | No | Whether to enable loop playback. |
| obeyMuteSwitch | boolean | true | No | Whether 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. |
| paused | boolean | Yes | Whether the playback is paused or stopped (read only). | |
| src | string | No | The audio file address which can be used to play the audio file directly. | |
| startTime | number | 0 | No | The position where the playback starts (in sec). |
| volume | number | 1 | No | Specifies 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
| Property | Type |
|---|---|
| position | number |
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
| Property | Type |
|---|---|
| callback | () => void |
onPlay
Listens on the audio playback event.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
onPause
Listens on the audio pause event.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
onStop
Listens on the event of stopping audio playback.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
onEnded
Listens on the event of playing an audio file to the end without interruption.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
onTimeUpdate
Listens on the event of updating audio playback progress.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
onError
Listens on the audio playback error event.
(callback?: (res: onErrorDetail) => void) => void
| Property | Type |
|---|---|
| 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
| Property | Type |
|---|---|
| callback | () => void |
onSeeking
Listens on the event of jumping to a specific position in the audio file.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
onSeeked
Listens on the event of finishing the jump to a specific position in the audio file.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offCanplay
Un-listens on the event that an audio file is ready for playback.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offPlay
Un-listens on the audio playback event.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offPause
Un-listens on the audio pause event.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offStop
Un-listens on the event of stopping audio playback.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offEnded
Un-listens on the event of playing an audio file to the end without interruption.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offTimeUpdate
Un-listens on the event of updating audio playback progress.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offError
Un-listens on the audio playback error event.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offWaiting
Un-listens on the audio loading event.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offSeeking
Un-listens on the event of jumping to a specific position in the audio file.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
offSeeked
Un-listens on the event of finishing the jump to a specific position in the audio file.
(callback?: () => void) => void
| Property | Type |
|---|---|
| callback | () => void |
Parameters
onErrorDetail
| Property | Type | Description |
|---|---|---|
| errCode | number | Error code |
| errMsg | string | Error message |
onErrorDetailErrCode
Valid values of errCode
| Value | Description |
|---|---|
| 10001 | System error |
| 10002 | Network error |
| 10003 | File error |
| 10004 | Format error |
| -1 | Unknown 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
| API | WeChat Mini-Program | H5 | React 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 | ✔️ | ✔️ |