Starting from version 1078, the API prefix has been changed from hbs to qg. The original prefix hbs is still supported.
API | Description |
|---|---|
Obtains an audio object for audio playback. During the playback, an audioID is returned, which can be used to operate the corresponding audio object. Each time create is called, a new object is created. Currently, up to five audio playback objects are supported at the same time. | |
Plays an audio file. | |
Pauses the audio playback. | |
Stops the audio playback. | |
Goes to a specified position. | |
Destroys the current instance. | |
Listens for audio playback progress update events. | |
Stops listening for audio playback progress update events. | |
Listens for the event that the audio finishes playing. | |
Stops listening for the event that the audio finishes playing. | |
Listens for audio playback events. | |
Stops listening for audio playback events. | |
Listens for audio pause events. | |
Stops listening for audio pause events. | |
Listens for audio stop events. | |
Stops listening for audio stop events. | |
Listens for audio playback error events. | |
Stops listening for audio playback error events. | |
Listens for the event that the audio becomes ready to play. This event does not guarantee smooth playback. | |
Stops listening for the event that the audio becomes ready to play. | |
Listens for audio loading events. This event occurs when audio playback is interrupted due to insufficient data and buffering is required. | |
Stops listening for audio loading events. | |
Listens for audio seek events. | |
Stops listening for audio seek events. | |
Listens for audio seek completion events. | |
Stops listening for audio seek completion events. |
Obtains an audio object for audio playback. During the playback, an audioID is returned, which can be used to operate the corresponding audio object. Each time create is called, a new object is created. Currently, up to five audio playback objects are supported at the same time.
Attribute | Description |
|---|---|
string src | URI of the audio to be played. |
number startTime | Position from which audio playback starts, in seconds. The default value is 0. |
boolean autoplay | Indicates whether the audio should play automatically. The default value is false. |
boolean loop | Indicates whether the audio should play cyclically. The default value is false. |
boolean obeyMuteSwitch | Indicates whether to respect the system mute switch. The default value is true. When this parameter is set to false, the audio is still on even if the system mute switch is enabled. |
number volume | Volume. The value ranges from 0 to 1. The default value is 1. |
number duration | Duration of the current audio, in seconds. A value is returned only when src is valid. The value is read-only. |
number currentTime | Current audio playback position, in seconds. A value is returned only when src is valid. The value is read-only and contains up to six decimal places. |
boolean paused | Indicates whether the audio playback is paused or stopped. The value is read-only. |
number buffered | Buffered end position of the audio. It ensures that only the content from the current playback position to the end point has been buffered. The value is read-only. |
const innerAudioContext = qg.createInnerAudioContext()
innerAudioContext.loop = true;
innerAudioContext.src = "https://47.98.62.68/cocos-runtime-demo/media/StreamAudio.mp3";
innerAudioContext.startTime = 0;
innerAudioContext.onPlay(() => {
console.log('Playback started.')
})
innerAudioContext.onPause(() => {
console.log('Playback paused.')
})
innerAudioContext.onStop(() => {
console.log('Playback stopped.')
})
innerAudioContext.onCanplay(() => {
console.log('Audio is ready to play.')
})
innerAudioContext.onWaiting(() => {
console.log('Waiting for audio data to load.')
})
innerAudioContext.onEnded(() => {
console.log('Playback ended.')
})
innerAudioContext.onSeeking(() => {
console.log('Seeking operation triggered.')
})
innerAudioContext.onSeeked(() => {
console.log('Seeking operation completed.')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
innerAudioContext.play(); Parameter | Type | Mandatory | Description |
|---|---|---|---|
position | number | Yes | Playback position to seek to, in seconds. The value is accurate to three decimal places, that is, millisecond-level precision. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Listener function for audio playback progress update events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Listener function passed to onTimeUpdate. |
const listener = function() { console.log('onTimeUpdate') }
InnerAudioContext.onTimeUpdate(listener)
InnerAudioContext.offTimeUpdate(listener) // The function object must be the same as that used during listening. Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio playback end events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio playback end events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio playback events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio playback events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio pause events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio pause events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio stop events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio stop events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio playback error events. |
Parameter | Type | Description |
|---|---|---|
errCode | number | Result code for audio playback error events. For details, please refer to the description. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio playback error events. |
Listens for the event that the audio becomes ready to play. This event does not guarantee smooth playback.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for the event that the audio becomes ready to play. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for the event that the audio becomes ready to play. |
Listens for audio loading events. This event occurs when audio playback is interrupted due to insufficient data and buffering is required.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio loading events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio loading events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio seek events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for audio seek events. |