Represents a component used to play audio files and control their playback states.
Since: 1.0.16
Related document: has.createAudioContext
Attributes
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| id | string | - | No | Unique ID of the audio component. |
| src | string | - | No | Resource address of the audio to be played. Currently, online addresses, project paths, and sandbox paths are supported. |
| loop | boolean | false | No | Whether to play in a loop. |
| controls | boolean | false | No | Whether to display default components. |
| poster | string | - | No | Resource address of the audio cover image on the default component. Currently, online addresses, project paths, and sandbox paths are supported. |
| name | string | - | No | Audio name on the default component. |
| author | string | - | No | Author name on the default component. |
| binderror | eventhandle | - | No | Event triggered when an error occurs, with detail = {errMsg: MediaError.code}. |
| bindplay | eventhandle | - | No | Event triggered when the playback starts or resumes. |
| bindpause | eventhandle | - | No | Event triggered when the playback is paused. |
| bindtimeupdate | eventhandle | - | No | Event triggered when the playback progress changes, with detail = {currentTime, duration}. |
| bindended | eventhandle | - | No | Event triggered when the playback reaches the end. |
event.detail returned by binderror
| Field | Description |
|---|---|
| errMsg | Error message. Return values: - 'MEDIA_ERR_ABORTED': Resource obtaining has been aborted. - 'MEDIA_ERR_NETWORK': Network error. - 'MEDIA_ERR_DECODE': Decoding error. - 'MEDIA_ERR_SRC_NOT_SUPPORTED': The resource is not supported. |
event.detail returned by bindtimeupdate
| Field | Description |
|---|---|
| currentTime | Current playback position, in seconds. |
| duration | Total duration, in seconds. |
Example:
HXML file:
<audio
id="myAudio"
poster="{{ posterImg }}"
src="{{ srcStr }}"
controls
loop
binderror="errorHandle"
bindplay="playHandle"
bindpause="pauseHandle"
bindtimeupdate="timeupdateHandle"
bindended="endedHandle"
></audio> JavaScript file:
Page({
data: {
posterImg: 'https://www.example.com/example.jpg', // Replace the example with the actual image resource address.
srcStr: 'https://www.example.com/test.mp3', // Replace the example with the actual audio resource address.
},
errorHandle(e) {
console.error('audio error', e.detail);
},
playHandle(e) {
console.info('audio play', e);
},
pauseHandle(e) {
console.info('audio pause', e);
},
timeupdateHandle(e) {
console.info('audio timeupdate', e.detail);
},
endedHandle(e) {
console.info('audio ended', e);
}
}); Effect
