提交 e007a13e 编写于 作者: G Gloria

Update docs against 13533

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 9261fe12
# AVPlayer Development
## Introduction
The AVPlayer converts audio or video resources into audible analog signals or renderable images and plays the signals or images using output devices. You can manage playback tasks on the AVPlayer. For example, you can control the playback (start/pause/stop/seek), set the volume, obtain track information, and release resources.
## Working Principles
The following figures show the [AVPlayer state](../reference/apis/js-apis-media.md#avplayerstate9) transition and interaction with external audio and video playback modules.
**Figure 1** AVPlayer state transition
![en-us_image_avplayer_state_machine](figures/en-us_image_avplayer_state_machine.png)
**Figure 2** Interaction with external modules for audio playback
![en-us_image_avplayer_audio](figures/en-us_image_avplayer_audio.png)
**NOTE**: When an application calls the **AVPlayer** JS APIs at the JS interface layer to implement a feature, the framework layer parses the resources into audio data streams through the playback service of the player framework. The audio data streams are then decoded by software and output to the audio service of the audio framework. The audio framework outputs the audio data streams to the audio HDI at the hardware interface layer to implement audio playback. A complete audio playback process requires the cooperation of the application (application adaptation required), player framework, audio framework, and audio HDI (driver adaptation required).
1. An application passes a URL into the **AVPlayer** JS API.
2. The playback service outputs the audio PCM data streams to the audio service, and the audio service outputs the data streams to the audio HDI.
**Figure 3** Interaction with external modules for video playback
![en-us_image_avplayer_video](figures/en-us_image_avplayer_video.png)
**NOTE**: When an application calls the **AVPlayer** JS APIs at the JS interface layer to implement a feature, the framework layer parses the resources into separate audio data streams and video data streams through the playback service of the player framework. The audio data streams are then decoded by software and output to the audio service of the audio framework. The audio framework outputs the audio data streams to the audio HDI at the hardware interface layer to implement audio playback. The video data streams are then decoded by hardware (recommended) or software and output to the renderer service of the graphic framework. The renderer service outputs the video data streams to the display HDI at the hardware interface layer. A complete video playback process requires the cooperation of the application (application adaptation required), XComponent, player framework, graphic framework, audio framework, display HDI (driver adaptation required), and audio HDI (driver adaptation required).
1. An application obtains the surface ID from the XComponent. For details about the obtaining method, see [XComponent](../reference/arkui-ts/ts-basic-components-xcomponent.md).
2. The application passes a URL and the surface ID into the **AVPlayer** JS API.
3. The playback service outputs video elementary streams (ESs) to the codec HDI, which decodes the ESs to obtain video frames (NV12/NV21/RGBA).
4. The playback service outputs the audio PCM data streams to the audio service, and the audio service outputs the data streams to the audio HDI.
5. The playback service outputs video frames (NV12/NV21/RGBA) to the renderer service, and the renderer service outputs the video frames to the display HDI.
## Compatibility
Use the mainstream playback formats and resolutions, rather than custom ones to avoid playback failures, frame freezing, and artifacts. The system will not be affected by incompatibility issues. If such an issue occurs, you can exit stream playback.
The table below lists the mainstream playback formats and resolutions.
| Video Container Format| Description | Resolution |
| :----------: | :-----------------------------------------------: | :--------------------------------: |
| mp4 | Video format: H.264/MPEG-2/MPEG-4/H.263; audio format: AAC/MP3| Mainstream resolutions, such as 1080p, 720p, 480p, and 270p|
| mkv | Video format: H.264/MPEG-2/MPEG-4/H.263; audio format: AAC/MP3| Mainstream resolutions, such as 1080p, 720p, 480p, and 270p|
| ts | Video format: H.264/MPEG-2/MPEG-4; audio format: AAC/MP3 | Mainstream resolutions, such as 1080p, 720p, 480p, and 270p|
| webm | Video format: VP8; audio format: VORBIS | Mainstream resolutions, such as 1080p, 720p, 480p, and 270p|
| Audio Container Format | Description |
| :----------: | :----------: |
| m4a | Audio format: AAC|
| aac | Audio format: AAC|
| mp3 | Audio format: MP3|
| ogg | Audio format: VORBIS |
| wav | Audio format: PCM |
## How to Develop
For details about the APIs, see the [AVPlayer APIs in the Media Class](../reference/apis/js-apis-media.md#avplayer9).
### Full-Process Scenario
The full playback process includes creating an instance, setting resources, setting a video window, preparing for playback, controlling playback, and resetting or releasing the resources. (During the preparation, you can obtain track information, volume, speed, focus mode, and zoom mode, and set bit rates. To control the playback, you can start, pause, and stop the playback, seek to a playback position, and set the volume.)
1. Call [createAVPlayer()](../reference/apis/js-apis-media.md#mediacreateavplayer9) to create an **AVPlayer** instance. The AVPlayer is initialized to the [idle](#avplayer_state) state.
2. Set the events to listen for, which will be used in the full-process scenario.
3. Set the resource [URL](../reference/apis/js-apis-media.md#avplayer_attributes). When the AVPlayer enters the [initialized](#avplayer_state) state, you can set the [surface ID](../reference/apis/js-apis-media.md#avplayer_attributes) for the video window. For details about the supported specifications, see [AVPlayer Attributes](../reference/apis/js-apis-media.md#avplayer_attributes).
4. Call [prepare()](../reference/apis/js-apis-media.md#avplayer_prepare) to switch the AVPlayer to the [prepared](#avplayer_state) state.
5. Perform video playback control. For example, you can call [play()](../reference/apis/js-apis-media.md#avplayer_play), [pause()](../reference/apis/js-apis-media.md#avplayer_pause), [seek()](../reference/apis/js-apis-media.md#avplayer_seek), and [stop()](../reference/apis/js-apis-media.md#avplayer_stop) to control the playback.
6. Call [reset()](../reference/apis/js-apis-media.md#avplayer_reset) to reset resources. The AVPlayer enters the [idle](#avplayer_state) state again, and you can change the resource [URL](../reference/apis/js-apis-media.md#avplayer_attributes).
7. Call [release()](../reference/apis/js-apis-media.md#avplayer_release) to release the instance. The AVPlayer enters the [released](#avplayer_state) state and exits the playback.
> **NOTE**
>
> When the AVPlayer is in the prepared, playing, paused, or completed state, the playback engine is working and a large amount of system running memory is occupied. If your application does not need to use the AVPlayer, call **reset()** or **release()** to release the resources.
### Listening Events
| Event Type | Description |
| ------------------------------------------------- | ------------------------------------------------------------ |
| stateChange<a name = stateChange></a> | Mandatory; used to listen for player state changes. |
| error<a name = error></a> | Mandatory; used to listen for player error information. |
| durationUpdate<a name = durationUpdate></a> | Used to listen for progress bar updates to refresh the resource duration. |
| timeUpdate<a name = timeUpdate></a> | Used to listen for the current position of the progress bar to refresh the current time. |
| seekDone<a name = seekDone></a> | Used to listen for the completion status of the **seek()** request. |
| speedDone<a name = speedDone></a> | Used to listen for the completion status of the **setSpeed()** request. |
| volumeChange<a name = volumeChange></a> | Used to listen for the completion status of the **setVolume()** request. |
| bitrateDone<a name = bitrateDone></a> | Used to listen for the completion status of the **setBitrate()** request, which is used for HTTP Live Streaming (HLS) streams. |
| availableBitrates<a name = availableBitrates></a> | Used to listen for available bit rates of HLS resources. The available bit rates are provided for **setBitrate()**. |
| bufferingUpdate<a name = bufferingUpdate></a> | Used to listen for network playback buffer information. |
| startRenderFrame<a name = startRenderFrame></a> | Used to listen for the rendering time of the first frame during video playback. |
| videoSizeChange<a name = videoSizeChange></a> | Used to listen for the width and height of video playback and adjust the window size and ratio.|
| audioInterrupt<a name = audioInterrupt></a> | Used to listen for audio interruption during video playback. This event is used together with the **audioInterruptMode** attribute.|
### Full-Process Scenario API Example
```js
import media from '@ohos.multimedia.media'
import audio from '@ohos.multimedia.audio';
import fileIO from '@ohos.fileio'
const TAG = 'AVPlayerDemo:'
export class AVPlayerDemo {
private count:number = 0
private avPlayer
private surfaceID:string // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API.
// Set AVPlayer callback functions.
setAVPlayerCallback() {
// Callback function for state changes.
this.avPlayer.on('stateChange', async (state, reason) => {
switch (state) {
case 'idle': // This state is reported upon a successful callback of reset().
console.info(TAG + 'state idle called')
this.avPlayer.release() // Release the AVPlayer instance.
break;
case 'initialized': // This state is reported when the AVPlayer sets the playback source.
console.info(TAG + 'state initialized called ')
this.avPlayer.surfaceId = this.surfaceID // Set the image to be displayed. This setting is not required when a pure audio resource is to be played.
this.avPlayer.prepare().then(() => {
console.info(TAG+ 'prepare success');
}, (err) => {
console.error(TAG + 'prepare filed,error message is :' + err.message)
})
break;
case 'prepared': // This state is reported upon a successful callback of prepare().
console.info(TAG + 'state prepared called')
this.avPlayer.play() // Call play() to start playback.
break;
case 'playing': // This state is reported upon a successful callback of play().
console.info(TAG + 'state playing called')
if (this.count == 0) {
this.avPlayer.pause() // Call pause() to pause the playback.
} else {
this.avPlayer.seek(10000, media.SeekMode.SEEK_PREV_SYNC) // Seek to 10 seconds. The seekDone callback is triggered.
}
break;
case 'paused': // This state is reported upon a successful callback of pause().
console.info(TAG + 'state paused called')
if (this.count == 0) {
this.count++
this.avPlayer.play() // Call play() to continue the playback.
}
break;
case 'completed': // This state is reported upon the completion of the playback.
console.info(TAG + 'state completed called')
this.avPlayer.stop() // Call stop() to stop the playback.
break;
case 'stopped': // This state is reported upon a successful callback of stop().
console.info(TAG + 'state stopped called')
this.avPlayer.reset() // Call reset() to initialize the AVPlayer state.
break;
case 'released':
console.info(TAG + 'state released called')
break;
case 'error':
console.info(TAG + 'state error called')
break;
default:
console.info(TAG + 'unkown state :' + state)
break;
}
})
// Callback function for time updates.
this.avPlayer.on('timeUpdate', (time:number) => {
console.info(TAG + 'timeUpdate success,and new time is :' + time)
})
// Callback function for volume updates.
this.avPlayer.on('volumeChange', (vol:number) => {
console.info(TAG + 'volumeChange success,and new volume is :' + vol)
this.avPlayer.setSpeed(media.AVPlayerSpeed.SPEED_FORWARD_2_00_X) // Double the playback speed. The speedDone callback is triggered.
})
// Callback function for the video playback completion event.
this.avPlayer.on('endOfStream', () => {
console.info(TAG + 'endOfStream success')
})
// Callback function for the seek operation.
this.avPlayer.on('seekDone', (seekDoneTime:number) => {
console.info(TAG + 'seekDone success,and seek time is:' + seekDoneTime)
this.avPlayer.setVolume(0.5) // Set the volume to 0.5. The volumeChange callback is triggered.
})
// Callback function for the speed setting operation.
this.avPlayer.on('speedDone', (speed:number) => {
console.info(TAG + 'speedDone success,and speed value is:' + speed)
})
// Callback function for successful bit rate setting.
this.avPlayer.on('bitrateDone', (bitrate:number) => {
console.info(TAG + 'bitrateDone success,and bitrate value is:' + bitrate)
})
// Callback function for buffering updates.
this.avPlayer.on('bufferingUpdate', (infoType: media.BufferingInfoType, value: number) => {
console.info(TAG + 'bufferingUpdate success,and infoType value is:' + infoType + ', value is :' + value)
})
// Callback function invoked when frame rendering starts.
this.avPlayer.on('startRenderFrame', () => {
console.info(TAG + 'startRenderFrame success')
})
// Callback function for video width and height changes.
this.avPlayer.on('videoSizeChange', (width: number, height: number) => {
console.info(TAG + 'videoSizeChange success,and width is:' + width + ', height is :' + height)
})
// Callback function for the audio interruption event.
this.avPlayer.on('audioInterrupt', (info: audio.InterruptEvent) => {
console.info(TAG + 'audioInterrupt success,and InterruptEvent info is:' + info)
})
// Callback function to report the available bit rates of HLS.
this.avPlayer.on('availableBitrates', (bitrates: Array<number>) => {
console.info(TAG + 'availableBitrates success,and availableBitrates length is:' + bitrates.length)
})
}
async avPlayerDemo() {
// Create an AVPlayer instance.
this.avPlayer = await media.createAVPlayer()
let fdPath = 'fd://'
let pathDir = "/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\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
let path = pathDir + '/H264_AAC.mp4'
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber
console.info('open fd success fd is' + fdPath)
}, (err) => {
console.info('open fd failed err is' + err)
}).catch((err) => {
console.info('open fd failed err is' + err)
});
this.avPlayer.url = fdPath
}
}
```
### Normal Playback Scenario
```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
const TAG = 'AVPlayerDemo:'
export class AVPlayerDemo {
private avPlayer
private surfaceID:string // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API.
// Set AVPlayer callback functions.
setAVPlayerCallback() {
// Callback function for state changes.
this.avPlayer.on('stateChange', async (state, reason) => {
switch (state) {
case 'idle': // This state is reported upon a successful callback of reset().
console.info(TAG + 'state idle called')
break;
case 'initialized': // This state is reported when the AVPlayer sets the playback source.
console.info(TAG + 'state initialized called ')
this.avPlayer.surfaceId = this.surfaceID // Set the image to be displayed. This setting is not required when a pure audio resource is to be played.
this.avPlayer.prepare().then(() => {
console.info(TAG+ 'prepare success');
}, (err) => {
console.error(TAG + 'prepare filed,error message is :' + err.message)
})
break;
case 'prepared': // This state is reported upon a successful callback of prepare().
console.info(TAG + 'state prepared called')
this.avPlayer.play() // Call play() to start playback.
break;
case 'playing': // This state is reported upon a successful callback of play().
console.info(TAG + 'state playing called')
break;
case 'paused': // This state is reported upon a successful callback of pause().
console.info(TAG + 'state paused called')
break;
case 'completed': // This state is reported upon the completion of the playback.
console.info(TAG + 'state completed called')
this.avPlayer.stop() // Call stop() to stop the playback.
break;
case 'stopped': // This state is reported upon a successful callback of stop().
console.info(TAG + 'state stopped called')
this.avPlayer.reset() // Call reset() to initialize the AVPlayer state.
break;
case 'released':
console.info(TAG + 'state released called')
break;
case 'error':
console.info(TAG + 'state error called')
break;
default:
console.info(TAG + 'unkown state :' + state)
break;
}
})
}
async avPlayerDemo() {
// Create an AVPlayer instance.
this.avPlayer = await media.createAVPlayer()
let fdPath = 'fd://'
let pathDir = "/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\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
let path = pathDir + '/H264_AAC.mp4'
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber
console.info('open fd success fd is' + fdPath)
}, (err) => {
console.info('open fd failed err is' + err)
}).catch((err) => {
console.info('open fd failed err is' + err)
});
this.avPlayer.url = fdPath
}
}
```
### Switching to the Next Video Clip
```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
const TAG = 'AVPlayerDemo:'
export class AVPlayerDemo {
private count:number = 0
private avPlayer
private surfaceID:string // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API.
// Set AVPlayer callback functions.
setAVPlayerCallback() {
// Callback function for state changes.
this.avPlayer.on('stateChange', async (state, reason) => {
switch (state) {
case 'idle': // This state is reported upon a successful callback of reset().
console.info(TAG + 'state idle called')
break;
case 'initialized': // This state is reported when the AVPlayer sets the playback source.
console.info(TAG + 'state initialized called ')
this.avPlayer.surfaceId = this.surfaceID // Set the image to be displayed. This setting is not required when a pure audio resource is to be played.
this.avPlayer.prepare().then(() => {
console.info(TAG+ 'prepare success');
}, (err) => {
console.error(TAG + 'prepare filed,error message is :' + err.message)
})
break;
case 'prepared': // This state is reported upon a successful callback of prepare().
console.info(TAG + 'state prepared called')
this.avPlayer.loop = true // Set the AVPlayer to loop a single item. The endOfStream callback is triggered when the previous round of the playback is complete.
this.avPlayer.play() // Call play() to start playback.
break;
case 'playing': // This state is reported upon a successful callback of play().
console.info(TAG + 'state playing called')
break;
case 'paused': // This state is reported upon a successful callback of pause().
console.info(TAG + 'state paused called')
break;
case 'completed': // This state is reported upon the completion of the playback.
console.info(TAG + 'state completed called')
// Cancel the loop playback when the endOfStream callback is triggered for the second time. The completed state is reported when the next round of the playback is complete.
this.avPlayer.stop() // Call stop() to stop the playback.
break;
case 'stopped': // This state is reported upon a successful callback of stop().
console.info(TAG + 'state stopped called')
this.avPlayer.reset() // Call reset() to initialize the AVPlayer state.
break;
case 'released':
console.info(TAG + 'state released called')
break;
case 'error':
console.info(TAG + 'state error called')
break;
default:
console.info(TAG + 'unkown state :' + state)
break;
}
})
// Callback function for the video playback completion event.
this.avPlayer.on('endOfStream', () => {
console.info(TAG + 'endOfStream success')
if (this.count == 1) {
this.avPlayer.loop = false // Cancel loop playback.
} else {
this.count++
}
})
}
async avPlayerDemo() {
// Create an AVPlayer instance.
this.avPlayer = await media.createAVPlayer()
let fdPath = 'fd://'
let pathDir = "/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\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
let path = pathDir + '/H264_AAC.mp4'
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber
console.info('open fd success fd is' + fdPath)
}, (err) => {
console.info('open fd failed err is' + err)
}).catch((err) => {
console.info('open fd failed err is' + err)
});
this.avPlayer.url = fdPath
}
}
```
### Looping a Song
```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
const TAG = 'AVPlayerDemo:'
export class AVPlayerDemo {
private count:number = 0
private avPlayer
private surfaceID:string // The surfaceID parameter is used for screen display. Its value is obtained through the XComponent API.
async nextVideo() {
let fdPath = 'fd://'
let pathDir = "/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\H264_MP3.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
let path = pathDir + '/H264_MP3.mp4'
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber
console.info('open fd success fd is' + fdPath)
}, (err) => {
console.info('open fd failed err is' + err)
}).catch((err) => {
console.info('open fd failed err is' + err)
});
this.avPlayer.url = fdPath // The initialized state is reported again.
}
// Set AVPlayer callback functions.
setAVPlayerCallback() {
// Callback function for state changes.
this.avPlayer.on('stateChange', async (state, reason) => {
switch (state) {
case 'idle': // This state is reported upon a successful callback of reset().
console.info(TAG + 'state idle called')
await this.nextVideo() // Switch to the next video.
break;
case 'initialized': // This state is reported when the AVPlayer sets the playback source.
console.info(TAG + 'state initialized called ')
this.avPlayer.surfaceId = this.surfaceID // Set the image to be displayed. This setting is not required when a pure audio resource is to be played.
this.avPlayer.prepare().then(() => {
console.info(TAG+ 'prepare success');
}, (err) => {
console.error(TAG + 'prepare filed,error message is :' + err.message)
})
break;
case 'prepared': // This state is reported upon a successful callback of prepare().
console.info(TAG + 'state prepared called')
this.avPlayer.play() // Call play() to start playback.
break;
case 'playing': // This state is reported upon a successful callback of play().
console.info(TAG + 'state playing called')
break;
case 'paused': // This state is reported upon a successful callback of pause().
console.info(TAG + 'state paused called')
break;
case 'completed': // This state is reported upon the completion of the playback.
console.info(TAG + 'state completed called')
if (this.count == 0) {
this.count++
this.avPlayer.reset() // Call reset() to prepare for switching to the next video.
} else {
this.avPlayer.release() // Release the AVPlayer instance when the new video finishes playing.
}
break;
case 'stopped': // This state is reported upon a successful callback of stop().
console.info(TAG + 'state stopped called')
break;
case 'released':
console.info(TAG + 'state released called')
break;
case 'error':
console.info(TAG + 'state error called')
break;
default:
console.info(TAG + 'unkown state :' + state)
break;
}
})
}
async avPlayerDemo() {
// Create an AVPlayer instance.
this.avPlayer = await media.createAVPlayer()
let fdPath = 'fd://'
let pathDir = "/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\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
let path = pathDir + '/H264_AAC.mp4'
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber
console.info('open fd success fd is' + fdPath)
}, (err) => {
console.info('open fd failed err is' + err)
}).catch((err) => {
console.info('open fd failed err is' + err)
});
this.avPlayer.url = fdPath
}
}
```
# AVRecorder Development
## Introduction
The AVRecorder captures audio signals, receives video signals, encodes audio and video signals, and saves them to files. With the AVRecorder, you can easily implement audio and video recording, including starting, pausing, resuming, and stopping recording, and releasing resources. You can also specify parameters such as the encoding format, encapsulation format, and file path for recording.
## Working Principles
The following figures show the AVRecorder state transition and the interaction with external modules for audio and video recording.
**Figure 1** AVRecorder state transition
![en-us_image_video_recorder_state_machine](figures/en-us_image_avrecorder_state_machine.png)
**Figure 2** Interaction between external modules for audio and video recording
![en-us_image_video_recorder_zero](figures/en-us_image_avrecorder_module_interaction.png)
**NOTE**: During audio recording, the framework layer calls the audio subsystem through the media service of the native framework to capture audio data through the audio HDI, encodes and encapsulates the data by using software, and saves the data to a file. During video recording, the camera subsystem captures image data through the video HDI. The media service encodes the image data through the video encoding HDI and encapsulates the encoded image data into a file. With the AVRecorder, you can implement pure audio recording, pure video recording, and audio and video recording.
## Constraints
Before developing the recording feature, configure the permissions **ohos.permission.MICROPHONE** and **ohos.permission.CAMERA** for your application. For details, see [Permission Application Guide](../security/accesstoken-guidelines.md).
## How to Develop
For details about the AVRecorder APIs, see the [AVRecorder APIs in the Media Class](../reference/apis/js-apis-media.md#avrecorder9).
For details about the processes related to the media library, see [Media Library Management](../reference/apis/js-apis-medialibrary.md).
For details about the camera-related process, see [Camera Management](../reference/apis/js-apis-camera.md).
### Full-Process Scenario of Audio and Video Recording
The full audio and video recording process includes creating an instance, setting recording parameters, obtaining the input surface, starting, pausing, resuming, and stopping recording, and releasing resources.
The value range that can be set for the audio recording parameters is restricted by the codec performance of the device and the performance of the audio subsystem.
The video range that can be set for the video recording parameters is restricted by the codec performance of the device and the performance of the camera subsystem.
```
import media from '@ohos.multimedia.media'
import camera from '@ohos.multimedia.camera'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
export class AVRecorderDemo {
private testFdNumber; // Used to save the File Descriptor (FD) address.
// Obtain the FD corresponding to fileName of the recorded file. The media library capability is required. To use the media library, configure the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
async getFd(fileName) {
// For details about the implementation mode, see the media library documentation.
this.testFdNumber = "fd://" + fdNumber.toString(); // e.g. fd://54
}
// Error callback triggered in the case of an error in the promise mode.
failureCallback(error) {
console.info('error happened, error message is ' + error.message);
}
// Error callback triggered in the case of an exception in the promise mode.
catchCallback(error) {
console.info('catch error happened, error message is ' + error.message);
}
async AVRecorderDemo() {
let AVRecorder; // Assign a value to the empty AVRecorder instance upon a successful call of createAVRecorder().
let surfaceID; // The surface ID is obtained by calling getInputSurface and transferred to the videoOutput object of the camera.
await this.getFd('01.mp4');
// Configure the parameters related to audio and video recording.
let avProfile = {
audioBitrate : 48000,
audioChannels : 2,
audioCodec : media.CodecMimeType.AUDIO_AAC,
audioSampleRate : 48000,
fileFormat : media.ContainerFormatType.CFT_MPEG_4,
videoBitrate : 48000,
videoCodec : media.CodecMimeType.VIDEO_MPEG4,
videoFrameWidth : 640,
videoFrameHeight : 480,
videoFrameRate : 30
}
let avConfig = {
audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
profile : avProfile,
url : 'fd://',
rotation : 0,
location : { latitude : 30, longitude : 130 }
}
// Create an AVRecorder instance.
await media.createAVRecorder().then((recorder) => {
console.info('case createAVRecorder called');
if (typeof (recorder) != 'undefined') {
AVRecorder = recorder;
console.info('createAVRecorder success');
} else {
console.info('createAVRecorder failed');
}
}, this.failureCallback).catch(this.catchCallback);
// After the instance is created, use the on('stateChange') and on('error') callbacks to listen for state changes and errors.
AVRecorder.on('stateChange', async (state, reason) => {
console.info('case state has changed, new state is :' + state);
switch (state) {
// Your can set the desired behavior in different states as required.
case 'idle':
// This state is reported upon a successful call of rest() or create().
break;
case 'prepared':
// This state is reported upon a successful call of prepare().
break;
case 'started':
// This state is reported upon a successful call of start().
break;
case 'paused':
// This state is reported upon a successful call of pause().
break;
case 'stopped':
// This state is reported upon a successful call of stop().
break;
case 'released':
// This state is reported upon a successful call of release().
break;
case 'error':
// The error state indicates that an error occurs at the bottom layer. You must rectify the fault and create an AVRecorder instance again.
break;
default:
console.info('case state is unknown');
}
});
AVRecorder.on('error', (err) => {
// Listen for non-interface errors.
console.info('case avRecorder.on(error) called, errMessage is ' + err.message);
});
// Call prepare() to prepare for recording. The bottom layer determines whether to record audio, video, or audio and video based on the input parameters of prepare().
await AVRecorder.prepare(avConfig).then(() => {
console.info('prepare success');
}, this.failureCallback).catch(this.catchCallback);
// If video recording is involved, call getInputSurface to obtain the input surface and pass the returned surface ID to the related camera API.
await AVRecorder.getInputSurface().then((surface) => {
console.info('getInputSurface success');
surfaceID = surface; // The surfaceID is passed into createVideoOutput() of the camera as an input parameter.
}, this.failureCallback).catch(this.catchCallback);
// Video recording depends on camera-related APIs. The following operations can be performed only after the video output start API is invoked.
// Start video recording.
await AVRecorder.start().then(() => {
console.info('start success');
}, this.failureCallback).catch(this.catchCallback);
// Pause video recording before the video output stop API of the camera is invoked.
await AVRecorder.pause().then(() => {
console.info('pause success');
}, this.failureCallback).catch(this.catchCallback);
// Resume video recording after the video output start API of the camera is invoked.
await AVRecorder.resume().then(() => {
console.info('resume success');
}, this.failureCallback).catch(this.catchCallback);
// Stop video recording after the video output stop API of the camera is invoked.
await AVRecorder.stop().then(() => {
console.info('stop success');
}, this.failureCallback).catch(this.catchCallback);
// Reset the recording configuration.
await AVRecorder.reset().then(() => {
console.info('reset success');
}, this.failureCallback).catch(this.catchCallback);
// Disable the listeners. The configured callbacks will be invalid after release() is invoked, even if you do not call off().
AVRecorder.off('stateChange');
AVRecorder.off('error');
// Release the video recording resources and camera object resources.
await AVRecorder.release().then(() => {
console.info('release success');
}, this.failureCallback).catch(this.catchCallback);
// Set the AVRecorder instance to null.
AVRecorder = undefined;
surfaceID = undefined;
}
}
```
### Full-Process Scenario of Pure Audio Recording
The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources.
The value range that can be set for the audio recording parameters is restricted by the codec performance of the device and the performance of the audio subsystem.
```
import media from '@ohos.multimedia.media'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
export class AudioRecorderDemo {
private testFdNumber; // Used to save the FD address.
// Obtain the FD corresponding to fileName of the recorded file. The media library capability is required. To use the media library, configure the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
async getFd(fileName) {
// For details about the implementation mode, see the media library documentation.
this.testFdNumber = "fd://" + fdNumber.toString(); // e.g. fd://54
}
// Error callback triggered in the case of an error in the promise mode.
failureCallback(error) {
console.info('error happened, error message is ' + error.message);
}
// Error callback triggered in the case of an exception in the promise mode.
catchCallback(error) {
console.info('catch error happened, error message is ' + error.message);
}
async audioRecorderDemo() {
let audioRecorder; // Assign a value to the empty AudioRecorder instance upon a successful call of createAVRecorder().
await this.getFd('01.m4a');
// Configure the parameters related to audio recording.
let audioProfile = {
audioBitrate : 48000,
audioChannels : 2,
audioCodec : media.CodecMimeType.AUDIO_AAC,
audioSampleRate : 48000,
fileFormat : media.ContainerFormatType.CFT_MPEG_4,
}
let audioConfig = {
audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
profile : audioProfile,
url : this.testFdNumber,
rotation : 0,
location : { latitude : 30, longitude : 130 }
}
// Create an AudioRecorder instance.
await media.createAVRecorder().then((recorder) => {
console.info('case createAVRecorder called');
if (typeof (recorder) != 'undefined') {
audioRecorder = recorder;
console.info('createAudioRecorder success');
} else {
console.info('createAudioRecorder failed');
}
}, this.failureCallback).catch(this.catchCallback);
// After the instance is created, use the on('stateChange') and on('error') callbacks to listen for state changes and errors.
audioRecorder.on('stateChange', async (state, reason) => {
console.info('case state has changed, new state is :' + state);
switch (state) {
// Your can set the desired behavior in different states as required.
case 'idle':
// This state is reported upon a successful call of rest() or create().
break;
case 'prepared':
// This state is reported upon a successful call of prepare().
break;
case 'started':
// This state is reported upon a successful call of start().
break;
case 'paused':
// This state is reported upon a successful call of pause().
break;
case 'stopped':
// This state is reported upon a successful call of stop().
break;
case 'released':
// This state is reported upon a successful call of release().
break;
case 'error':
// The error state indicates that an error occurs at the bottom layer. You must rectify the fault and create an AudioRecorder instance again.
break;
default:
console.info('case state is unknown');
}
});
audioRecorder.on('error', (err) => {
// Listen for non-interface errors.
console.info('case avRecorder.on(error) called, errMessage is ' + err.message);
});
// Call prepare() to prepare for recording. The bottom layer determines whether to record audio, video, or audio and video based on the input parameters of prepare().
await audioRecorder.prepare(audioConfig).then(() => {
console.info('prepare success');
}, this.failureCallback).catch(this.catchCallback);
// Call start() to start audio recording.
await audioRecorder.start().then(() => {
console.info('start success');
}, this.failureCallback).catch(this.catchCallback);
// Call pause() to pause audio recording.
await audioRecorder.pause().then(() => {
console.info('pause success');
}, this.failureCallback).catch(this.catchCallback);
// Call resume() to resume audio recording.
await audioRecorder.resume().then(() => {
console.info('resume success');
}, this.failureCallback).catch(this.catchCallback);
// Call stop() to stop audio recording.
await audioRecorder.stop().then(() => {
console.info('stop success');
}, this.failureCallback).catch(this.catchCallback);
// Call reset() to reset the recording configuration.
await audioRecorder.reset().then(() => {
console.info('reset success');
}, this.failureCallback).catch(this.catchCallback);
// Disable the listeners. The configured callbacks will be invalid after release() is invoked, even if you do not call off().
avRecorder.off('stateChange');
avRecorder.off('error');
// Call release() to release audio recording resources.
await audioRecorder.release().then(() => {
console.info('release success');
}, this.failureCallback).catch(this.catchCallback);
// Set the AudioRecorder instance to null.
audioRecorder = undefined;
}
}
```
### Full-Process Scenario of Pure Video Recording
The full video recording process includes creating an instance, setting recording parameters, obtaining the input surface, starting, pausing, resuming, and stopping recording, and releasing resources.
The video range that can be set for the video recording parameters is restricted by the codec performance of the device and the performance of the camera subsystem.
```
import media from '@ohos.multimedia.media'
import camera from '@ohos.multimedia.camera'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
export class VideoRecorderDemo {
private testFdNumber; // Used to save the FD address.
// Obtain the FD corresponding to fileName of the recorded file. The media library capability is required. To use the media library, configure the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
async getFd(fileName) {
// For details about the implementation mode, see the media library documentation.
this.testFdNumber = "fd://" + fdNumber.toString(); // e.g. fd://54
}
// Error callback triggered in the case of an error in the promise mode.
failureCallback(error) {
console.info('error happened, error message is ' + error.message);
}
// Error callback triggered in the case of an exception in the promise mode.
catchCallback(error) {
console.info('catch error happened, error message is ' + error.message);
}
async videoRecorderDemo() {
let videoRecorder; // Assign a value to the empty VideoRecorder instance upon a successful call of createAVRecorder().
let surfaceID; // The surface ID is obtained by calling getInputSurface and transferred to the videoOutput object of the camera.
await this.getFd('01.mp4');
// Configure the parameters related to video recording.
let videoProfile = {
fileFormat : media.ContainerFormatType.CFT_MPEG_4,
videoBitrate : 48000,
videoCodec : media.CodecMimeType.VIDEO_MPEG4,
videoFrameWidth : 640,
videoFrameHeight : 480,
videoFrameRate : 30
}
let videoConfig = {
videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
profile : videoProfile,
url : 'fd://',
rotation : 0,
location : { latitude : 30, longitude : 130 }
}
// Create a VideoRecorder instance.
await media.createAVRecorder().then((recorder) => {
console.info('case createVideoRecorder called');
if (typeof (recorder) != 'undefined') {
videoRecorder = recorder;
console.info('createVideoRecorder success');
} else {
console.info('createVideoRecorder failed');
}
}, this.failureCallback).catch(this.catchCallback);
// After the instance is created, use the on('stateChange') and on('error') callbacks to listen for state changes and errors.
videoRecorder.on('stateChange', async (state, reason) => {
console.info('case state has changed, new state is :' + state);
switch (state) {
// Your can set the desired behavior in different states as required.
case 'idle':
// This state is reported upon a successful call of rest() or create().
break;
case 'prepared':
// This state is reported upon a successful call of prepare().
break;
case 'started':
// This state is reported upon a successful call of start().
break;
case 'paused':
// This state is reported upon a successful call of pause().
break;
case 'stopped':
// This state is reported upon a successful call of stop().
break;
case 'released':
// This state is reported upon a successful call of release().
break;
case 'error':
// The error state indicates that an error occurs at the bottom layer. You must rectify the fault and create a VideoRecorder instance again.
break;
default:
console.info('case state is unknown');
}
});
videoRecorder.on('error', (err) => {
// Listen for non-interface errors.
console.info('case avRecorder.on(error) called, errMessage is ' + err.message);
});
// Call prepare() to prepare for recording. The bottom layer determines whether to record audio, video, or audio and video based on the input parameters of prepare().
await videoRecorder.prepare(videoConfig).then(() => {
console.info('prepare success');
}, this.failureCallback).catch(this.catchCallback);
// If video recording is involved, call getInputSurface to obtain the input surface and pass the returned surface ID to the related camera API.
await videoRecorder.getInputSurface().then((surface) => {
console.info('getInputSurface success');
surfaceID = surface; // The surfaceID is passed into createVideoOutput() of the camera as an input parameter.
}, this.failureCallback).catch(this.catchCallback);
// Video recording depends on camera-related APIs. The following operations can be performed only after the video output start API is invoked.
// Start video recording.
await videoRecorder.start().then(() => {
console.info('start success');
}, this.failureCallback).catch(this.catchCallback);
// Pause video recording before the video output stop API of the camera is invoked.
await videoRecorder.pause().then(() => {
console.info('pause success');
}, this.failureCallback).catch(this.catchCallback);
// Resume video recording after the video output start API of the camera is invoked.
await videoRecorder.resume().then(() => {
console.info('resume success');
}, this.failureCallback).catch(this.catchCallback);
// Stop video recording after the video output stop API of the camera is invoked.
await videoRecorder.stop().then(() => {
console.info('stop success');
}, this.failureCallback).catch(this.catchCallback);
// Reset the recording configuration.
await videoRecorder.reset().then(() => {
console.info('reset success');
}, this.failureCallback).catch(this.catchCallback);
// Disable the listeners. The configured callbacks will be invalid after release() is invoked, even if you do not call off().
videoRecorder.off('stateChange');
videoRecorder.off('error');
// Release the video recording resources and camera object resources.
await videoRecorder.release().then(() => {
console.info('release success');
}, this.failureCallback).catch(this.catchCallback);
// Set the VideoRecorder instance to null.
videoRecorder = undefined;
surfaceID = undefined;
}
}
```
### AVRecorder App
The AVRecorder app provides a complete audio and video recording process, which includes creating an instance, setting recording parameters, obtaining the input surface, starting, pausing, resuming, and stopping recording, and releasing resources.
For details about the code, see [AVRecorderDemo]([multimedia_player_framework: Implementation of media playback and recording](https://gitee.com/openharmony/multimedia_player_framework/tree/master/test/appdemo/AVRecorderDemo)).
因为 它太大了无法显示 source diff 。你可以改为 查看blob
# Media Subsystem ChangeLog
## cl.media.1 API Change of the Playback Function
Added the [AVPlayer](../../../application-dev/reference/apis/js-apis-media.md#avplayer9)<sup>9+</sup> API for audio and video playback, with the updated state machine and error codes, which is recommended. The following APIs for audio playback and video playback are no longer maintained: [AudioPlayer](../../../application-dev/reference/apis/js-apis-media.md#audioplayer)<sup>6+</sup> and [VideoPlayer](../../../application-dev/reference/apis/js-apis-media.md#videoplayer)<sup>8+</sup>.
**Change Impacts**
The original APIs can still be used but are no longer maintained. You are advised to use the new API instead.
**Key API/Component Changes**
Added APIs
| Class | Declaration |
| -------------- | ------------------------------------------------------------ |
| media | createAVPlayer(callback: AsyncCallback\<AVPlayer>): void |
| media | createAVPlayer() : Promise\<AVPlayer> |
| media.AVPlayer | interface AVPlayer |
| media.AVPlayer | videoScaleType ?: VideoScaleType |
| media.AVPlayer | url ?: string |
| media.AVPlayer | surfaceId ?: string |
| media.AVPlayer | stop(callback: AsyncCallback\<void>): void |
| media.AVPlayer | stop(): Promise\<void> |
| media.AVPlayer | setVolume(volume: number): void |
| media.AVPlayer | setSpeed(speed: PlaybackSpeed): void |
| media.AVPlayer | setBitrate(bitrate: number): void |
| media.AVPlayer | seek(timeMs: number, mode?:SeekMode): void |
| media.AVPlayer | reset(callback: AsyncCallback\<void>): void |
| media.AVPlayer | reset(): Promise\<void> |
| media.AVPlayer | release(callback: AsyncCallback\<void>): void |
| media.AVPlayer | release(): Promise\<void> |
| media.AVPlayer | readonly width: number |
| media.AVPlayer | readonly state: AVPlayerState |
| media.AVPlayer | readonly height: number |
| media.AVPlayer | readonly duration: number |
| media.AVPlayer | readonly currentTime: number |
| media.AVPlayer | prepare(callback: AsyncCallback\<void>): void |
| media.AVPlayer | prepare(): Promise\<void> |
| media.AVPlayer | play(callback: AsyncCallback\<void>): void |
| media.AVPlayer | play(): Promise\<void> |
| media.AVPlayer | pause(callback: AsyncCallback\<void>): void |
| media.AVPlayer | pause(): Promise\<void> |
| media.AVPlayer | on(type: 'volumeChange', callback: Callback\<number>): void |
| media.AVPlayer | on(type: 'videoSizeChange', callback: (width: number, height: number) => void): void |
| media.AVPlayer | on(type: 'timeUpdate', callback: Callback\<number>): void |
| media.AVPlayer | on(type: 'stateChange', callback: (state: AVPlayerState, reason: StateChangeReason) => void): void |
| media.AVPlayer | on(type: 'startRenderFrame', callback: Callback\<void>): void |
| media.AVPlayer | on(type: 'speedDone', callback: Callback\<number>): void |
| media.AVPlayer | on(type: 'seekDone', callback: Callback\<number>): void |
| media.AVPlayer | on(type: 'error', callback: ErrorCallback): void |
| media.AVPlayer | on(type: 'endOfStream', callback: Callback\<void>): void |
| media.AVPlayer | on(type: 'durationUpdate', callback: Callback\<number>): void |
| media.AVPlayer | on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void |
| media.AVPlayer | on(type: 'bitrateDone', callback: Callback\<number>): void |
| media.AVPlayer | on(type: 'availableBitrates', callback: (bitrates: Array\<number>) => void): void |
| media.AVPlayer | on(type: 'audioInterrupt', callback: (info: audio.InterruptEvent) => void): void |
| media.AVPlayer | off(type: 'volumeChange'): void |
| media.AVPlayer | off(type: 'videoSizeChange'): void |
| media.AVPlayer | off(type: 'timeUpdate'): void |
| media.AVPlayer | off(type: 'stateChange'): void |
| media.AVPlayer | off(type: 'startRenderFrame'): void |
| media.AVPlayer | off(type: 'speedDone'): void |
| media.AVPlayer | off(type: 'seekDone'): void |
| media.AVPlayer | off(type: 'error'): void |
| media.AVPlayer | off(type: 'endOfStream'): void |
| media.AVPlayer | off(type: 'durationUpdate'): void |
| media.AVPlayer | off(type: 'bufferingUpdate'): void |
| media.AVPlayer | off(type: 'bitrateDone'): void |
| media.AVPlayer | off(type: 'availableBitrates'): void |
| media.AVPlayer | off(type: 'audioInterrupt'): void |
| media.AVPlayer | loop: boolean |
| media.AVPlayer | getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void |
| media.AVPlayer | getTrackDescription() : Promise\<Array\<MediaDescription>> |
| media.AVPlayer | fdSrc ?: AVFileDescriptor |
| media.AVPlayer | audioInterruptMode ?: audio.InterruptMode |
| unnamed | type AVPlayerState = 'idle' \| 'initialized' \| 'prepared' \| 'playing' \| 'paused' \| 'completed' \| 'stopped' \| 'released' \| 'error' |
APIs no longer maintained
| Class | Declaration |
| ----------------- | ------------------------------------------------------------ |
| media | createVideoPlayer(callback: AsyncCallback\<VideoPlayer>): void |
| media | createVideoPlayer() : Promise\<VideoPlayer> |
| media | createAudioPlayer(): AudioPlayer |
| media.AudioPlayer | interface AudioPlayer |
| media.AudioPlayer | play(): void |
| media.AudioPlayer | release(): void |
| media.AudioPlayer | audioInterruptMode ?: audio.InterruptMode |
| media.AudioPlayer | fdSrc: AVFileDescriptor |
| media.AudioPlayer | seek(timeMs: number): void |
| media.AudioPlayer | readonly duration: number |
| media.AudioPlayer | loop: boolean |
| media.AudioPlayer | readonly state: AudioState |
| media.AudioPlayer | getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void |
| media.AudioPlayer | getTrackDescription() : Promise\<Array\<MediaDescription>> |
| media.AudioPlayer | on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void |
| media.AudioPlayer | on(type: 'play' \| 'pause' \| 'stop' \| 'reset' \| 'dataLoad' \| 'finish' \| 'volumeChange', callback: () => void): void |
| media.AudioPlayer | on(type: 'timeUpdate', callback: Callback\<number>): void |
| media.AudioPlayer | on(type: 'audioInterrupt', callback: (info: audio.InterruptEvent) => void): void |
| media.AudioPlayer | on(type: 'error', callback: ErrorCallback): void |
| media.AudioPlayer | setVolume(vol: number): void |
| media.AudioPlayer | pause(): void |
| media.AudioPlayer | readonly currentTime: number |
| media.AudioPlayer | stop(): void |
| media.AudioPlayer | reset(): void |
| media.AudioPlayer | src: string |
| media.VideoPlayer | interface VideoPlayer |
| media.VideoPlayer | play(callback: AsyncCallback\<void>): void |
| media.VideoPlayer | play(): Promise\<void> |
| media.VideoPlayer | prepare(callback: AsyncCallback\<void>): void |
| media.VideoPlayer | prepare(): Promise\<void> |
| media.VideoPlayer | release(callback: AsyncCallback\<void>): void |
| media.VideoPlayer | release(): Promise\<void> |
| media.VideoPlayer | audioInterruptMode ?: audio.InterruptMode |
| media.VideoPlayer | fdSrc: AVFileDescriptor |
| media.VideoPlayer | seek(timeMs: number, callback: AsyncCallback\<number>): void |
| media.VideoPlayer | seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void |
| media.VideoPlayer | seek(timeMs: number, mode?:SeekMode): Promise\<number> |
| media.VideoPlayer | readonly duration: number |
| media.VideoPlayer | loop: boolean |
| media.VideoPlayer | videoScaleType ?: VideoScaleType |
| media.VideoPlayer | readonly state: VideoPlayState |
| media.VideoPlayer | getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void |
| media.VideoPlayer | getTrackDescription() : Promise\<Array\<MediaDescription>> |
| media.VideoPlayer | readonly height: number |
| media.VideoPlayer | on(type: 'playbackCompleted', callback: Callback\<void>): void |
| media.VideoPlayer | on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void |
| media.VideoPlayer | on(type: 'startRenderFrame', callback: Callback\<void>): void |
| media.VideoPlayer | on(type: 'videoSizeChanged', callback: (width: number, height: number) => void): void |
| media.VideoPlayer | on(type: 'audioInterrupt', callback: (info: audio.InterruptEvent) => void): void |
| media.VideoPlayer | on(type: 'error', callback: ErrorCallback): void |
| media.VideoPlayer | setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void |
| media.VideoPlayer | setDisplaySurface(surfaceId: string): Promise\<void> |
| media.VideoPlayer | setVolume(vol: number, callback: AsyncCallback\<void>): void |
| media.VideoPlayer | setVolume(vol: number): Promise\<void> |
| media.VideoPlayer | url: string |
| media.VideoPlayer | pause(callback: AsyncCallback\<void>): void |
| media.VideoPlayer | pause(): Promise\<void> |
| media.VideoPlayer | readonly currentTime: number |
| media.VideoPlayer | setSpeed(speed:number, callback: AsyncCallback\<number>): void |
| media.VideoPlayer | setSpeed(speed:number): Promise\<number> |
| media.VideoPlayer | stop(callback: AsyncCallback\<void>): void |
| media.VideoPlayer | stop(): Promise\<void> |
| media.VideoPlayer | readonly width: number |
| media.VideoPlayer | reset(callback: AsyncCallback\<void>): void |
| media.VideoPlayer | reset(): Promise\<void> |
| unnamed | type AudioState = 'idle' \| 'playing' \| 'paused' \| 'stopped' \| 'error' |
| unnamed | type VideoPlayState = 'idle' \| 'prepared' \| 'playing' \| 'paused' \| 'stopped' \| 'error' |
**Adaptation Guide**
For details, see the [reference](../../../application-dev/reference/apis/js-apis-media.md) for each API.
## cl.media.2 API Change of the Recording Function
Added the [AVRecorder](../../../application-dev/reference/apis/js-apis-media.md#avrecorder9)<sup>9+</sup> API for audio and video recording, with the updated state machine and error codes, which is recommended. The following APIs for audio recording and video recording are no longer maintained: [AudioRecorder](../../../application-dev/reference/apis/js-apis-media.md#audiorecorder)<sup>6+</sup> and [VideoRecorder](../../../application-dev/reference/apis/js-apis-media.md#videorecorder9)<sup>9+</sup>.
The [AudioSourceType](../../../application-dev/reference/apis/js-apis-media.md#audiosourcetype9) and [VideoSourceType](../../../application-dev/reference/apis/js-apis-media.md#videosourcetype9) APIs shared by the old and new recording APIs are changed to non-system APIs.
**Change Impacts**
The [AudioRecorder](../../../application-dev/reference/apis/js-apis-media.md#audiorecorder)<sup>6+</sup> and [VideoRecorder](../../../application-dev/reference/apis/js-apis-media.md#videorecorder9)<sup>9+</sup> APIs can still be used but are no longer maintained. You are advised to use the [AVRecorder](../../../application-dev/reference/apis/js-apis-media.md#avrecorder9)<sup>9+</sup> API instead.
**Key API/Component Changes**
Added APIs
| Class | Declaration |
| ----------------------- | ------------------------------------------------------------ |
| media | createAVRecorder(callback: AsyncCallback\<AVRecorder>): void |
| media | createAVRecorder() : Promise\<AVRecorder> |
| media.AVRecorder | interface AVRecorder |
| media.AVRecorder | prepare(config: AVRecorderConfig, callback: AsyncCallback\<void>): void |
| media.AVRecorder | prepare(config: AVRecorderConfig): Promise\<void> |
| media.AVRecorder | release(callback: AsyncCallback\<void>): void |
| media.AVRecorder | release(): Promise\<void> |
| media.AVRecorder | readonly state: AVRecorderState |
| media.AVRecorder | on(type: 'stateChange', callback: (state: AVRecorderState, reason: StateChangeReason) => void): void |
| media.AVRecorder | on(type: 'error', callback: ErrorCallback): void |
| media.AVRecorder | resume(callback: AsyncCallback\<void>): void |
| media.AVRecorder | resume(): Promise\<void> |
| media.AVRecorder | start(callback: AsyncCallback\<void>): void |
| media.AVRecorder | start(): Promise\<void> |
| media.AVRecorder | off(type: 'stateChange'): void |
| media.AVRecorder | off(type: 'error'): void |
| media.AVRecorder | pause(callback: AsyncCallback\<void>): void |
| media.AVRecorder | pause(): Promise\<void> |
| media.AVRecorder | stop(callback: AsyncCallback\<void>): void |
| media.AVRecorder | stop(): Promise\<void> |
| media.AVRecorder | reset(callback: AsyncCallback\<void>): void |
| media.AVRecorder | reset(): Promise\<void> |
| media.AVRecorder | getInputSurface(callback: AsyncCallback\<string>): void |
| media.AVRecorder | getInputSurface(): Promise\<string> |
| media.AVRecorderConfig | videoSourceType?: VideoSourceType |
| media.AVRecorderConfig | audioSourceType?: AudioSourceType |
| media.AVRecorderConfig | profile: AVRecorderProfile |
| media.AVRecorderConfig | rotation?: number |
| media.AVRecorderConfig | url: string |
| media.AVRecorderConfig | location?: Location |
| media.AVRecorderConfig | interface AVRecorderConfig |
| media.AVRecorderProfile | videoBitrate?: number |
| media.AVRecorderProfile | videoCodec?: CodecMimeType |
| media.AVRecorderProfile | audioCodec?: CodecMimeType |
| media.AVRecorderProfile | videoFrameRate?: number |
| media.AVRecorderProfile | videoFrameHeight?: number |
| media.AVRecorderProfile | audioSampleRate?: number |
| media.AVRecorderProfile | audioBitrate?: number |
| media.AVRecorderProfile | videoFrameWidth?: number |
| media.AVRecorderProfile | audioChannels?: number |
| media.AVRecorderProfile | fileFormat: ContainerFormatType |
| media.AVRecorderProfile | interface AVRecorderProfile |
| unnamed | type AVRecorderState = 'idle' \| 'prepared' \| 'started' \| 'paused' \| 'stopped' \| 'released' \| 'error' |
APIs no longer maintained
| Class | Declaration |
| -------------------------- | ------------------------------------------------------------ |
| media | createVideoRecorder(callback: AsyncCallback\<VideoRecorder>): void |
| media | createVideoRecorder(): Promise\<VideoRecorder> |
| media | createAudioRecorder(): AudioRecorder |
| media.AudioRecorder | interface AudioRecorder |
| media.AudioRecorder | prepare(config: AudioRecorderConfig): void |
| media.AudioRecorder | release(): void |
| media.AudioRecorder | on(type: 'prepare' \| 'start' \| 'pause' \| 'resume' \| 'stop' \| 'release' \| 'reset', callback: () => void): void |
| media.AudioRecorder | on(type: 'error', callback: ErrorCallback): void |
| media.AudioRecorder | resume(): void |
| media.AudioRecorder | start(): void |
| media.AudioRecorder | pause(): void |
| media.AudioRecorder | stop(): void |
| media.AudioRecorder | reset(): void |
| media.AudioRecorderConfig | audioSampleRate?: number |
| media.AudioRecorderConfig | location?: Location |
| media.AudioRecorderConfig | fileFormat?: ContainerFormatType |
| media.AudioRecorderConfig | interface AudioRecorderConfig |
| media.AudioRecorderConfig | audioEncoder?: AudioEncoder |
| media.AudioRecorderConfig | audioEncodeBitRate?: number |
| media.AudioRecorderConfig | numberOfChannels?: number |
| media.AudioRecorderConfig | format?: AudioOutputFormat |
| media.AudioRecorderConfig | uri: string |
| media.AudioRecorderConfig | audioEncoderMime?: CodecMimeType |
| media.VideoRecorder | interface VideoRecorder |
| media.VideoRecorder | prepare(config: VideoRecorderConfig, callback: AsyncCallback\<void>): void |
| media.VideoRecorder | prepare(config: VideoRecorderConfig): Promise\<void> |
| media.VideoRecorder | release(callback: AsyncCallback\<void>): void |
| media.VideoRecorder | release(): Promise\<void> |
| media.VideoRecorder | readonly state: VideoRecordState |
| media.VideoRecorder | on(type: 'error', callback: ErrorCallback): void |
| media.VideoRecorder | resume(callback: AsyncCallback\<void>): void |
| media.VideoRecorder | resume(): Promise\<void> |
| media.VideoRecorder | start(callback: AsyncCallback\<void>): void |
| media.VideoRecorder | start(): Promise\<void> |
| media.VideoRecorder | pause(callback: AsyncCallback\<void>): void |
| media.VideoRecorder | pause(): Promise\<void> |
| media.VideoRecorder | stop(callback: AsyncCallback\<void>): void |
| media.VideoRecorder | stop(): Promise\<void> |
| media.VideoRecorder | reset(callback: AsyncCallback\<void>): void |
| media.VideoRecorder | reset(): Promise\<void> |
| media.VideoRecorder | getInputSurface(callback: AsyncCallback\<string>): void |
| media.VideoRecorder | getInputSurface(): Promise\<string> |
| media.VideoRecorderConfig | videoSourceType: VideoSourceType |
| media.VideoRecorderConfig | audioSourceType?: AudioSourceType |
| media.VideoRecorderConfig | profile: VideoRecorderProfile |
| media.VideoRecorderConfig | rotation?: number |
| media.VideoRecorderConfig | url: string |
| media.VideoRecorderConfig | location?: Location |
| media.VideoRecorderConfig | interface VideoRecorderConfig |
| media.VideoRecorderProfile | readonly videoBitrate: number |
| media.VideoRecorderProfile | readonly videoCodec: CodecMimeType |
| media.VideoRecorderProfile | readonly audioCodec: CodecMimeType |
| media.VideoRecorderProfile | readonly videoFrameRate: number |
| media.VideoRecorderProfile | readonly videoFrameHeight: number |
| media.VideoRecorderProfile | readonly audioSampleRate: number |
| media.VideoRecorderProfile | readonly audioBitrate: number |
| media.VideoRecorderProfile | readonly videoFrameWidth: number |
| media.VideoRecorderProfile | readonly audioChannels: number |
| media.VideoRecorderProfile | readonly fileFormat: ContainerFormatType |
| media.VideoRecorderProfile | interface VideoRecorderProfile |
| unnamed | type VideoRecordState = 'idle' \| 'prepared' \| 'playing' \| 'paused' \| 'stopped' \| 'error' |
Changed APIs
| Class | Declaration | Capability Before Change | Capability After Change | Whether a System API Before Change| Whether a System API After Change|
| --------------------- | ------------------------------------------------------------ | ----------------------------------------------- | -------------------------------------------- | -------------------- | -------------------- |
| media.AudioSourceType | enum AudioSourceType { /** * default audio source type. * @since 9 * @syscap SystemCapability.Multimedia.Media.AVRecorder */ AUDIO_SOURCE_TYPE_DEFAULT = 0, /** * source type mic. * @since 9 * @syscap SystemCapability.Multimedia.Media.AVRecorder */ AUDIO_SOURCE_TYPE_MIC = 1, } | SystemCapability.Multimedia.Media.VideoRecorder | SystemCapability.Multimedia.Media.AVRecorder | Yes | No |
| media.VideoSourceType | enum VideoSourceType { /** * surface raw data. * @since 9 * @syscap SystemCapability.Multimedia.Media.AVRecorder */ VIDEO_SOURCE_TYPE_SURFACE_YUV = 0, /** * surface ES data. * @since 9 * @syscap SystemCapability.Multimedia.Media.AVRecorder */ VIDEO_SOURCE_TYPE_SURFACE_ES = 1, } | SystemCapability.Multimedia.Media.VideoRecorder | SystemCapability.Multimedia.Media.AVRecorder | Yes | No |
**Adaptation Guide**
For details, see the [reference](../../../application-dev/reference/apis/js-apis-media.md) for each API.
## cl.media.3 Error Code Change
Added the standard error code enumeration type [AVErrorCode9](../../../application-dev/reference/apis/js-apis-media.md#averrorcode)<sup>9+</sup> that replaces the original error code enumeration type [MediaErrorCode](../../../application-dev/reference/apis/js-apis-media.md#mediaerrorcode)<sup>8+</sup>.
**Change Impacts**
The error code enumeration type [MediaErrorCode](../../../application-dev/reference/apis/js-apis-media.md#mediaerrorcode)<sup>8+</sup> is still used for original APIs. [AVErrorCode9](../../../application-dev/reference/apis/js-apis-media.md#averrorcode)<sup>9+</sup> is used for newly added APIs.
**Key API/Component Changes**
Added API
| Class | Declaration |
| ----------------- | ------------------------------------------------------------ |
| media.AVErrorCode | enum AVErrorCode { /** * operation success. * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_OK = 0, /** * permission denied. * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_NO_PERMISSION = 201, /** * invalid parameter. * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_INVALID_PARAMETER = 401, /** * the api is not supported in the current version * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_UNSUPPORT_CAPABILITY = 801, /** * the system memory is insufficient or the number of services reaches the upper limit * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_NO_MEMORY = 5400101, /** * current status does not allow or do not have permission to perform this operation * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_OPERATE_NOT_PERMIT = 5400102, /** * data flow exception information * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_IO = 5400103, /** * system or network response timeout. * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_TIMEOUT = 5400104, /** * service process died. * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_SERVICE_DIED = 5400105, /** * unsupported media format * @since 9 * @syscap SystemCapability.Multimedia.Media.Core */ AVERR_UNSUPPORT_FORMAT = 5400106, } |
API no longer maintained
| Class | Declaration |
| -------------------- | ------------------------------------------------------------ |
| media.MediaErrorCode | enum MediaErrorCode { /** * operation success. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_OK = 0, /** * malloc or new memory failed. maybe system have no memory. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_NO_MEMORY = 1, /** * no permission for the operation. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_OPERATION_NOT_PERMIT = 2, /** * invalid argument. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_INVALID_VAL = 3, /** * an I/O error occurred. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_IO = 4, /** * operation time out. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_TIMEOUT = 5, /** * unknown error. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_UNKNOWN = 6, /** * media service died. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_SERVICE_DIED = 7, /** * operation is not permit in current state. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_INVALID_STATE = 8, /** * operation is not supported in current version. * @since 8 * @syscap SystemCapability.Multimedia.Media.Core */ MSERR_UNSUPPORTED = 9, } |
<!--no_check-->
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册