has.playBackgroundAudio(Object object)
Uses a background player to play music.
The music keeps playing when you switch to the home screen. However, if another application or atomic service starts playing audio or video, the original playback will pause.
Since: 1.0.9
Required permissions: To enable background audio playback, perform the following operations:
Declare the ohos.permission.KEEP_BACKGROUND_RUNNING permission in module.json5.
Declare the backgroundModes configuration item in module.json5.
module.json5 file:
"module": {
"abilities": [
{
"backgroundModes": [
"audioPlayback"
]
}
]
} app.json file:
"requiredBackgroundModes": ["audio"]
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| dataUrl | string | Yes | Music link. The supported formats are m4a, aac, mp3, ogg, wav, flac, and amr. Internet links and local paths (starting with internal://) are supported. |
| title | string | No | Music title. |
| coverImgUrl | string | No | URL of the cover image. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
| 102 | dataUrl is invalid or inaccessible. |
| 70002205 | state is not play. |
| 70002202 | AVPlayer play error. |
| 70002206 | AVPlayer can not reset. |
| 70002203 | AVPlayer reset error. |
| 70002201 | playBackgroundAudio error. For details, see Media Error Codes. |
Example:
has.playBackgroundAudio({
dataUrl: 'https://www.example.com/test.mp3', // Replace the example URL with the actual URL.
title: 'Music title',
coverImgUrl: 'https://www.example.com/test.png', // Replace the example URL with the actual URL.
success: () => {
console.info('playBackgroundAudio success');
},
fail: (err) => {
console.error('playBackgroundAudio fail', err);
},
complete: (res) => {
console.info('playBackgroundAudio complete', res);
}
}); has.pauseBackgroundAudio(Object object)
Pauses music playback.
Since: 1.0.9
Parameters
The input parameter is an object containing the following fields.
| Attribute | Type | Mandatory | Description |
|---|---|---|---|
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
| 102 | AVPlayer can not be paused. |
| 70002402 | AVPlayer pause error. |
| 70002401 | pauseBackgroundAudio error. For details, see Media Error Codes. |
Example:
has.pauseBackgroundAudio({
success: () => {
console.info('pauseBackgroundAudio success');
},
fail: (err) => {
console.error('pauseBackgroundAudio fail', err);
},
complete: (res) => {
console.info('pauseBackgroundAudio complete', res);
}
}); has.seekBackgroundAudio(Object object)
Seeks to a specified playback position.
Since: 1.0.9
Parameters
The input parameter is an object containing the following fields.
| Attribute | Type | Mandatory | Description |
|---|---|---|---|
| position | number | Yes | Target position, in seconds. The value is accurate to three decimal places, supporting millisecond-level accuracy. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
| 70002302 | state is not seek. |
| 70002301 | seekBackgroundAudio error. For details, see Media Error Codes. |
Example:
has.seekBackgroundAudio({
position: 30,
success: () => {
console.info('seekBackgroundAudio success');
},
fail: (err) => {
console.error('seekBackgroundAudio fail', err);
},
complete: (res) => {
console.info('seekBackgroundAudio complete', res);
}
}); has.stopBackgroundAudio(Object object)
Stops music playback.
Since: 1.0.9
Parameters
The input parameter is an object containing the following fields.
| Attribute | Type | Mandatory | Description |
|---|---|---|---|
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
| 102 | AVPlayer can not be stop. |
| 70002502 | AVPlayer stop error. |
| 70002501 | stopBackgroundAudio error. For details, see Media Error Codes. |
Example:
has.stopBackgroundAudio({
success: () => {
console.info('stopBackgroundAudio success');
},
fail: (err) => {
console.error('stopBackgroundAudio fail', err);
},
complete: (res) => {
console.info('stopBackgroundAudio complete', res);
}
}); has.getBackgroundAudioPlayerState(Object object)
Obtains the background music playback state.
Since: 1.0.9
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| duration | number | Duration of the selected audio, in seconds. A value is returned only when music is playing. |
| currentPosition | number | Playback position of the selected audio, in seconds. A value is returned only when music is playing. |
| status | number | Playback state. 0: paused. 1: playing. 2: No music is playing. |
| downloadPercent | number | Download progress of the audio file, in percentage. A value is returned only when music is playing. |
| dataUrl | string | URL of the music data. A value is returned only when music is playing. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | AVPlayer is not initialized. |
| 70002101 | getBackgroundAudioPlayerState error. |
Example:
has.getBackgroundAudioPlayerState({
success: (res) => {
console.info('getBackgroundAudioPlayerState success', res);
},
fail: (err) => {
console.error('getBackgroundAudioPlayerState fail', err);
},
complete: (res) => {
console.info('getBackgroundAudioPlayerState complete', res);
}
}); has.onBackgroundAudioPlay(function callback)
Listens for music playback events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for music playback events. |
Example:
has.onBackgroundAudioPlay(() => {
console.info('onBackgroundAudioPlay callback triggered');
}); has.onBackgroundAudioPause(function callback)
Listens for music pause events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for music pause events. |
Example:
has.onBackgroundAudioPause(() => {
console.info('onBackgroundAudioPause callback triggered');
}); has.onBackgroundAudioStop(function callback)
Listens for music stop events.
Since: 1.0.9
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for music stop events. |
Example:
has.onBackgroundAudioStop(() => {
console.info('onBackgroundAudioStop callback triggered');
}); has.getBackgroundAudioManager(): BackgroundAudioManager
Obtains a globally unique background audio manager. It supports background audio playback. If the audio is playing when the atomic service transitions to the background, playback will continue uninterrupted.
Since: 1.0.9
Required permissions: To enable background audio playback, perform the following operations:
Declare the ohos.permission.KEEP_BACKGROUND_RUNNING permission in module.json5.
Declare the backgroundModes configuration item in module.json5.
module.json5 file:
"module": {
"abilities": [
{
"backgroundModes": [
"audioPlayback"
]
}
]
} Configure the requiredBackgroundModes attribute in app.json.
app.json file:
"requiredBackgroundModes": ["audio"]
Returns:
BackgroundAudioManager object.
Example:
const backgroundAudioManager = has.getBackgroundAudioManager();