You can use audio playback APIs to convert audio data into audible analog signals and play the signals using output devices. You can also manage playback tasks. For example, you can start, suspend, stop playback, release resources, set the volume, seek to a playback position, and obtain track information.
You can use audio playback APIs to convert audio data into audible analog signals and play the signals using output devices. You can also manage playback tasks. For example, you can control the playback and volume, obtain track information, and release resources.
## Working Principles
The following figures show the audio playback status changes and the interaction with external modules for audio playback.
The following figures show the audio playback state transition and the interaction with external modules for audio playback.
**Figure 1** Audio playback state transition
...
...
@@ -28,7 +28,7 @@ For details about the APIs, see [AudioPlayer in the Media API](../reference/apis
> **NOTE**
>
> The method for obtaining the path in the FA model is different from that in the stage model. **pathDir** used in the sample code below is an example. You need to obtain the path based on project requirements. For details about how to obtain the path, see [Application Sandbox Path Guidelines](../reference/apis/js-apis-fileio.md#guidelines).
> The method for obtaining the path in the FA model is different from that in the stage model. For details about how to obtain the path, see [Application Sandbox Path Guidelines](../reference/apis/js-apis-fileio.md#guidelines).
### Full-Process Scenario
...
...
@@ -109,7 +109,7 @@ async function audioPlayerDemo() {
setCallBack(audioPlayer);// Set the event callbacks.
// 2. Set the URI of the audio file.
letfdPath='fd://'
letpathDir="/data/storage/el2/base/haps/entry/files"// The method for obtaining pathDir in the FA model is different from that in the stage model. For details, see NOTE just below How to Develop. You need to obtain pathDir based on project requirements.
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letpath=pathDir+'/01.mp3'
awaitfileIO.open(path).then((fdNumber)=>{
...
...
@@ -151,7 +151,7 @@ export class AudioDemo {
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
letfdPath='fd://'
letpathDir="/data/storage/el2/base/haps/entry/files"// The method for obtaining pathDir in the FA model is different from that in the stage model. For details, see NOTE just below How to Develop. You need to obtain pathDir based on project requirements.
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letpath=pathDir+'/01.mp3'
awaitfileIO.open(path).then((fdNumber)=>{
...
...
@@ -199,7 +199,7 @@ export class AudioDemo {
asyncnextMusic(audioPlayer){
this.isNextMusic=true;
letnextFdPath='fd://'
letpathDir="/data/storage/el2/base/haps/entry/files"// The method for obtaining pathDir in the FA model is different from that in the stage model. For details, see NOTE just below How to Develop. You need to obtain pathDir based on project requirements.
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\02.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letnextpath=pathDir+'/02.mp3'
awaitfileIO.open(nextpath).then((fdNumber)=>{
...
...
@@ -217,7 +217,7 @@ export class AudioDemo {
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
letfdPath='fd://'
letpathDir="/data/storage/el2/base/haps/entry/files"// The method for obtaining pathDir in the FA model is different from that in the stage model. For details, see NOTE just below How to Develop. You need to obtain pathDir based on project requirements.
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letpath=pathDir+'/01.mp3'
awaitfileIO.open(path).then((fdNumber)=>{
...
...
@@ -256,7 +256,7 @@ export class AudioDemo {
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
letfdPath='fd://'
letpathDir="/data/storage/el2/base/haps/entry/files"// The method for obtaining pathDir in the FA model is different from that in the stage model. For details, see NOTE just below How to Develop. You need to obtain pathDir based on project requirements.
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The multimedia subsystem provides a set of simple and easy-to-use APIs for you to access the system and use media resources.
...
...
@@ -52,7 +53,7 @@ Creates a **VideoPlayer** instance. This API uses an asynchronous callback to re
| callback | AsyncCallback<[VideoPlayer](#videoplayer8)> | Yes | Callback used to return the result. If the operation is successful, the **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video media.|
| callback | AsyncCallback<[VideoPlayer](#videoplayer8)> | Yes | Callback used to return the result. If the operation is successful, the **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video.|
**Example**
...
...
@@ -81,7 +82,7 @@ Creates a **VideoPlayer** instance. This API uses a promise to return the result
| Promise<[VideoPlayer](#videoplayer8)> | Promise used to return the result. If the operation is successful, the **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video media.|
| Promise<[VideoPlayer](#videoplayer8)> | Promise used to return the result. If the operation is successful, the **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video.|
**Example**
...
...
@@ -113,7 +114,7 @@ Only one **AudioRecorder** instance can be created per device.
| [AudioRecorder](#audiorecorder) | Returns the **AudioRecorder** instance if the operation is successful; returns **null** otherwise. The instance can be used to record audio media.|
| [AudioRecorder](#audiorecorder) | Returns the **AudioRecorder** instance if the operation is successful; returns **null** otherwise. The instance can be used to record audio.|
**Example**
...
...
@@ -130,11 +131,21 @@ Only one **AudioRecorder** instance can be created per device.
| callback | AsyncCallback<[VideoRecorder](#videorecorder9)> | Yes | Callback used to return the result. If the operation is successful, the **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video media.|
| callback | AsyncCallback<[VideoRecorder](#videorecorder9)> | Yes | Callback used to return the result. If the operation is successful, the **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video.|
**Error codes**
For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).
| ID| Error Message |
| -------- | ------------------------------ |
| 5400101 | No memory. Return by callback. |
**Example**
...
...
@@ -160,11 +171,21 @@ Only one **AudioRecorder** instance can be created per device.
| Promise<[VideoRecorder](#videorecorder9)> | Promise used to return the result. If the operation is successful, the **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video media.|
| Promise<[VideoRecorder](#videorecorder9)> | Promise used to return the result. If the operation is successful, the **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video.|
**Error codes**
For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).
| ID| Error Message |
| -------- | ----------------------------- |
| 5400101 | No memory. Return by promise. |
**Example**
...
...
@@ -277,7 +298,7 @@ For details about the audio playback demo, see [Audio Playback Development](../.
| src | string | Yes | Yes | Audio file URI. The mainstream audio formats (M4A, AAC, MPEG-3, OGG, and WAV) are supported.<br>**Examples of supported URI schemes**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.INTERNET|
| fdSrc<sup>9+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes | Yes | Description of the audio file. This attribute is required when audio resources of an application are continuously stored in a file.<br>**Example:**<br>Assume that a music file that stores continuous music resources consists of the following:<br>Music 1 (address offset: 0, byte length: 100)<br>Music 2 (address offset: 101; byte length: 50)<br>Music 3 (address offset: 151, byte length: 150)<br>1. To play music 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play music 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play music 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>If the file is an independent music file, use **src=fd://xx**.<br>|
| fdSrc<sup>9+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes | Yes | Description of the audio file. This attribute is required when audio resources of an application are continuously stored in a file.<br>**Example:**<br>Assume that a music file that stores continuous music resources consists of the following:<br>Music 1 (address offset: 0, byte length: 100)<br>Music 2 (address offset: 101; byte length: 50)<br>Music 3 (address offset: 151, byte length: 150)<br>1. To play music 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play music 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play music 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>To play an independent music file, use **src=fd://xx**.<br>|
| loop | boolean | Yes | Yes | Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite. |
@@ -669,7 +690,7 @@ Describes audio and video file resources. It is used to specify a particular res
## VideoPlayer<sup>8+</sup>
Provides APIs to manage and play video. Before calling an API of **VideoPlayer**, you must call[createVideoPlayer()](#mediacreatevideoplayer8) to create a [VideoPlayer](#videoplayer8) instance.
Provides APIs to manage and play video. Before calling an API of **VideoPlayer**, you must use[createVideoPlayer()](#mediacreatevideoplayer8) to create a [VideoPlayer](#videoplayer8) instance.
For details about the video playback demo, see [Video Playback Development](../../media/video-playback.md).
...
...
@@ -679,7 +700,7 @@ For details about the video playback demo, see [Video Playback Development](../.
| url<sup>8+</sup> | string | Yes | Yes | Video media URL. The mainstream video formats (MPEG-4, MPEG-TS, WebM, and MKV) are supported.<br>**Example of supported URIs**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>|
| url<sup>8+</sup> | string | Yes | Yes | Video URL. The mainstream video formats (MPEG-4, MPEG-TS, WebM, and MKV) are supported.<br>**Example of supported URIs**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>|
| fdSrc<sup>9+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes| Yes| Description of a video file. This attribute is required when video resources of an application are continuously stored in a file.<br>**Example:**<br>Assume that a music file that stores continuous music resources consists of the following:<br>Video 1 (address offset: 0, byte length: 100)<br>Video 2 (address offset: 101; byte length: 50)<br>Video 3 (address offset: 151, byte length: 150)<br>1. To play video 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play video 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play video 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>To play an independent video file, use **src=fd://xx**.<br>|
| loop<sup>8+</sup> | boolean | Yes | Yes | Whether to loop video playback. The value **true** means to loop video playback, and **false** means the opposite. |
Selects a bit rate from available ones, which can be obtained by calling [availableBitratesCollect](#onavailablebitratescollect9). This API uses an asynchronous callback to return the result.
| bitrate | number | Yes | Bit rate to select, which is used in the HLS multi-bit rate scenario. The unit is bit/s. |
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the set value is returned, the operation is successful; otherwise, the operation fails.|
**Example**
```js
letbitrate=1024000;
videoPlayer.selectBitrate(bitrate,(err,result)=>{
if(err==null){
console.info('selectBitrate success!');
}else{
console.info('selectBitrate fail!');
}
});
```
### selectBitrate<sup>9+</sup>
selectBitrate(bitrate:number): Promise\<number>
Selects a bit rate from available ones, which can be obtained by calling [availableBitratesCollect](#onavailablebitratescollect9). This API uses a promise to return the result.
Implements audio recording. Before calling an API of **AudioRecorder**, you must call[createAudioRecorder()](#mediacreateaudiorecorder) to create an [AudioRecorder](#audiorecorder) instance.
Implements audio recording. Before calling an API of **AudioRecorder**, you must use[createAudioRecorder()](#mediacreateaudiorecorder) to create an [AudioRecorder](#audiorecorder) instance.
For details about the audio recording demo, see [Audio Recording Development](../../media/audio-recorder.md).
...
...
@@ -1890,13 +1852,13 @@ Enumerates the audio encoding formats.
| Promise\<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).
| ID| Error Message |
| -------- | -------------------------------- |
| 5400103 | IO error. Return by promise. |
| 5400105 | Service died. Return by promise. |
**Example**
```js
...
...
@@ -2444,6 +2596,15 @@ Subscribes to video recording error events. After an error event is reported, yo
| type | string | Yes | Event type, which is **'error'** in this case.<br>The **'error'** event is triggered when an error occurs during video recording.|
| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. |
**Error codes**
For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).
| ID| Error Message |
| -------- | --------------------------------- |
| 5400103 | IO error. Return by callback. |
| 5400105 | Service died. Return by callback. |
**Example**
```js
...
...
@@ -2459,6 +2620,8 @@ Enumerates the video recording states. You can obtain the state through the **st