Implements background audio management.
Since: 1.0.9
Attributes
| Attribute | Type | Default Value | Mandatory | Read-Only | Description |
|---|---|---|---|---|---|
| src | string | - | Yes | No | Audio source address. When a new source is set, the audio is automatically played. The supported formats are m4a, aac, mp3, ogg, wav, flac, and amr. Internet links and local paths (starting with internal://) are supported. |
| startTime | number | 0 | No | No | Position where audio playback starts, in seconds. |
| title | string | Atomic service name | No | No | Audio title, which is used as the title of the system audio player. The default value is the name of the atomic service. |
| epname | string | '' | No | No | Album name, which is used as the subtitle of the system audio player only when the singer name is empty. |
| singer | string | '' | No | No | Singer name, which is used as the subtitle of the system audio player. |
| coverImgUrl | string | Atomic service icon | No | No | URL of the cover image, which is used as the background image of the system audio player. By default, the value is the icon of the atomic service. |
| webUrl | string | '' | No | No | Page URL. |
| protocol | string | http | No | No | Audio protocol. The default value is http. If this parameter is set to HLS, live audio in HLS format can be played. |
| playbackRate | number | 1 | No | No | Recording playback speed. The supported values are 0.125, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, and 2. |
| duration | number | - | No | Yes | Total audio duration, in seconds. |
| currentTime | number | - | No | Yes | Current audio playback position, in seconds. |
| paused | boolean | - | No | Yes | Whether the audio is paused or stopped. |
| buffered | number | - | No | Yes | Buffered duration of the audio, in seconds. |
BackgroundAudioManager.play(): void
Plays music.
Since: 1.0.9
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.title = 'Title'; backgroundAudioManager.src = 'https://www.example.com/test.mp3'; // Replace the example URL with the actual URL. backgroundAudioManager.play();
BackgroundAudioManager.pause(): void
Pauses music.
Since: 1.0.9
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.title = 'Title'; backgroundAudioManager.src = 'https://www.example.com/test.mp3'; // Replace the example URL with the actual URL. backgroundAudioManager.pause();
BackgroundAudioManager.seek(number currentTime): void
Seeks to a specified playback position.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| currentTime | number | Yes | Target position, in seconds. The value is accurate to three decimal places, supporting millisecond-level accuracy. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.title = 'Title'; backgroundAudioManager.src = 'https://www.example.com/test.mp3'; // Replace the example URL with the actual URL. backgroundAudioManager.seek(10.5);
BackgroundAudioManager.stop(): void
Stops music.
Since: 1.0.9
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.title = 'Title'; backgroundAudioManager.src = 'https://www.example.com/test.mp3'; // Replace the example URL with the actual URL. backgroundAudioManager.stop();
BackgroundAudioManager.onPlay(function callback)
Listens for background audio playback events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for background audio playback events. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onPlay(() => {
console.info('BackgroundAudioManager onPlay callback triggered');
}); BackgroundAudioManager.offPlay(function callback)
Cancels listening for background audio playback events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onPlay. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offPlay();
BackgroundAudioManager.onPause(function callback)
Listens for background audio pause events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for background audio pause events. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onPause(() => {
console.info('BackgroundAudioManager onPause callback triggered');
}); BackgroundAudioManager.offPause(function callback)
Cancels listening for background audio pause events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onPause. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offPause();
BackgroundAudioManager.onSeeking(function callback)
Listens for the event indicating that the background audio playback starts seeking.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the event indicating that the background audio playback starts seeking. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onSeeking(() => {
console.info('BackgroundAudioManager onSeeking callback triggered');
}); BackgroundAudioManager.offSeeking(function callback)
Cancels listening for the event indicating that the background audio playback starts seeking.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onSeeking. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offSeeking();
BackgroundAudioManager.onSeeked(function callback)
Listens for the event indicating that the background audio playback finishes seeking.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the event indicating that the background audio playback finishes seeking. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onSeeked(() => {
console.info('BackgroundAudioManager onSeeked callback triggered');
}); BackgroundAudioManager.offSeeked(function callback)
Cancels listening for the event indicating that the background audio playback finishes seeking.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onSeeked. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offSeeked();
BackgroundAudioManager.onEnded(function callback)
Listens for the event indicating that the background audio playback ends.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the event indicating that the background audio playback ends. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onEnded(() => {
console.info('BackgroundAudioManager onEnded callback triggered');
}); BackgroundAudioManager.offEnded(function callback)
Cancels listening for the event indicating that the background audio playback ends.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onEnded. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offEnded();
BackgroundAudioManager.onStop(function callback)
Listens for background audio stop events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for background audio stop events. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onStop(() => {
console.info('BackgroundAudioManager onStop callback triggered');
}); BackgroundAudioManager.offStop(function callback)
Cancels listening for background audio stop events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onStop. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offStop();
BackgroundAudioManager.onTimeUpdate(function callback)
Listens for the event indicating that the background audio playback progress is updated.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the event indicating that the background audio playback progress is updated. |
Return value of callback
The return value is a number, which indicates the current background audio playback position, in seconds. The value is accurate to three decimal places.
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onTimeUpdate((currentTime) => {
console.info('BackgroundAudioManager onTimeUpdate callback triggered', currentTime);
}); BackgroundAudioManager.offTimeUpdate(function callback)
Cancels listening for the event indicating that the background audio playback progress is updated.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onTimeUpdate. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offTimeUpdate();
BackgroundAudioManager.onError(function callback)
Listens for background audio playback error events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for background audio playback error events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| errCode | number | Error code. |
| errMsg | string | Error message. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onError((err) => {
console.error('BackgroundAudioManager onError callback triggered', err);
}); BackgroundAudioManager.offError(function callback)
Cancels listening for background audio playback error events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onError. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offError();
BackgroundAudioManager.onCanplay(function callback)
Listens for the event indicating that the background audio is ready for playback.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the event indicating that the background audio is ready for playback. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onCanplay(() => {
console.info('BackgroundAudioManager onCanplay callback triggered');
}); BackgroundAudioManager.offCanplay(function callback)
Cancels listening for the event indicating that the background audio is ready for playback.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onCanplay. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offCanplay();
BackgroundAudioManager.onPrev(function callback)
Listens for the event indicating that the user taps the previous button on the system music playback panel.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the event indicating that the user taps the previous button on the system music playback panel. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onPrev(() => {
console.info('BackgroundAudioManager onPrev callback triggered');
}); BackgroundAudioManager.offPrev(function callback)
Cancels listening for the event indicating that the user taps the previous button on the system music playback panel.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onPrev. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offPrev();
BackgroundAudioManager.onNext(function callback)
Listens for the event indicating that the user taps the next button on the system music playback panel.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the event indicating that the user taps the next button on the system music playback panel. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();
backgroundAudioManager.onNext(() => {
console.info('BackgroundAudioManager onNext callback triggered');
}); BackgroundAudioManager.offNext(function callback)
Cancels listening for the event indicating that the user taps the next button on the system music playback panel.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by BackgroundAudioManager.onNext. If this parameter is not passed, all listener functions are removed. |
Example:
const backgroundAudioManager = has.getBackgroundAudioManager(); backgroundAudioManager.offNext();