提交 3323f4f5 编写于 作者: W wusongqing

Updated docs

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 aa5001c3
...@@ -25,6 +25,9 @@ The full audio playback process includes creating an instance, setting the URI, ...@@ -25,6 +25,9 @@ The full audio playback process includes creating an instance, setting the URI,
For details about the **src** media source input types supported by **AudioPlayer**, see the [src attribute](../reference/apis/js-apis-media.md#audioplayer_Attributes). For details about the **src** media source input types supported by **AudioPlayer**, see the [src attribute](../reference/apis/js-apis-media.md#audioplayer_Attributes).
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) { function SetCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully. audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
console.info('audio set source success'); console.info('audio set source success');
...@@ -80,7 +83,18 @@ function printfDescription(obj) { ...@@ -80,7 +83,18 @@ function printfDescription(obj) {
let audioPlayer = media.createAudioPlayer(); let audioPlayer = media.createAudioPlayer();
SetCallBack(audioPlayer); // Set the event callbacks. SetCallBack(audioPlayer); // Set the event callbacks.
// 2. Set the URI of the audio file. // 2. Set the URI of the audio file.
audioPlayer.src = 'file:///data/data/ohos.xxx.xxx/files/test.mp3'; // Set the src attribute and trigger the 'dataLoad' event callback. let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback.
// 3. Play the audio. // 3. Play the audio.
audioPlayer.play(); // The play() method can be invoked only after the 'dataLoad' event callback is complete. The 'play' event callback is triggered. audioPlayer.play(); // The play() method can be invoked only after the 'dataLoad' event callback is complete. The 'play' event callback is triggered.
// 4. Seek to the playback position. // 4. Seek to the playback position.
...@@ -111,6 +125,9 @@ audioPlayer = undefined; ...@@ -111,6 +125,9 @@ audioPlayer = undefined;
### Normal Playback Scenario ### Normal Playback Scenario
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) { function SetCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully. audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
console.info('audio set source success'); console.info('audio set source success');
...@@ -128,13 +145,27 @@ function SetCallBack(audioPlayer) { ...@@ -128,13 +145,27 @@ function SetCallBack(audioPlayer) {
let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance. let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance.
SetCallBack(audioPlayer); // Set the event callbacks. SetCallBack(audioPlayer); // Set the event callbacks.
/* Set the URI of the audio file selected by the user. */ /* Set the FD (local playback) of the audio file selected by the user. */
audioPlayer.src = 'file:///data/data/ohos.xxx.xxx/files/test.mp3'; // Set the src attribute and trigger the 'dataLoad' event callback. let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback.
``` ```
### Switching to the Next Song ### Switching to the Next Song
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) { function SetCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully. audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
console.info('audio set source success'); console.info('audio set source success');
...@@ -152,16 +183,42 @@ function SetCallBack(audioPlayer) { ...@@ -152,16 +183,42 @@ function SetCallBack(audioPlayer) {
let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance. let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance.
SetCallBack(audioPlayer); // Set the event callbacks. SetCallBack(audioPlayer); // Set the event callbacks.
/* Set the URI of the audio file selected by the user. */ /* Set the FD (local playback) of the audio file selected by the user. */
audioPlayer.src = 'file:///data/data/ohos.xxx.xxx/files/test.mp3'; // Set the src attribute and trigger the 'dataLoad' event callback. let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback.
/* Send the instruction to switch to the next song after a period of time. */ /* Send the instruction to switch to the next song after a period of time. */
audioPlayer.reset(); audioPlayer.reset();
audioPlayer.src = 'file:///data/data/ohos.xxx.xxx/files/next.mp3';
/* Set the FD (local playback) of the audio file selected by the user. */
let fdNextPath = 'fd://'
let nextPath = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(nextPath).then(fdNumber) => {
fdNextPath = fdNextPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdNextPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdNextPath;
``` ```
### Looping a Song ### Looping a Song
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) { function SetCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully. audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
console.info('audio set source success'); console.info('audio set source success');
...@@ -179,7 +236,19 @@ function SetCallBack(audioPlayer) { ...@@ -179,7 +236,19 @@ function SetCallBack(audioPlayer) {
let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance. let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance.
SetCallBack(audioPlayer); // Set the event callbacks. SetCallBack(audioPlayer); // Set the event callbacks.
/* Set the URI of the audio file selected by the user. */
audioPlayer.src = 'file:///data/data/ohos.xxx.xxx/files/test.mp3'; // Set the src attribute and trigger the 'dataLoad' event callback. /* Set the FD (local playback) of the audio file selected by the user. */
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback.
audioPlayer.loop = true; // Set the loop playback attribute. audioPlayer.loop = true; // Set the loop playback attribute.
``` ```
...@@ -23,6 +23,11 @@ For details about the APIs used for audio recording, see [js-apis-media.md](../r ...@@ -23,6 +23,11 @@ For details about the APIs used for audio recording, see [js-apis-media.md](../r
The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources. The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources.
```js ```js
import media from '@ohos.multimedia.media'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
let testFdNumber;
function SetCallBack(audioRecorder) { function SetCallBack(audioRecorder) {
audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. audioRecorder.on('prepare', () => { // Set the 'prepare' event callback.
console.log('prepare success'); console.log('prepare success');
...@@ -57,6 +62,31 @@ function SetCallBack(audioRecorder) { ...@@ -57,6 +62,31 @@ function SetCallBack(audioRecorder) {
}); });
} }
// pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Movies/01.mp3.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
async function getFd(pathName) {
let displayName = pathName;
const mediaTest = mediaLibrary.getMediaLibrary();
let fileKeyObj = mediaLibrary.FileKey;
let mediaType = mediaLibrary.MediaType.VIDEO;
let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
if (dataUri != undefined) {
let args = dataUri.id.toString();
let fetchOp = {
selections : fileKeyObj.ID + "=?",
selectionArgs : [args],
}
let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
let fileAsset = await fetchFileResult.getAllObject();
let fdNumber = await fileAsset[0].open('Rw');
fdNumber = "fd://" + fdNumber.toString();
testFdNumber = fdNumber;
}
}
await getFd('01.mp3');
// 1. Create an AudioRecorder instance. // 1. Create an AudioRecorder instance.
let audioRecorder = media.createAudioRecorder(); let audioRecorder = media.createAudioRecorder();
// 2. Set the callbacks. // 2. Set the callbacks.
...@@ -68,7 +98,7 @@ let audioRecorderConfig = { ...@@ -68,7 +98,7 @@ let audioRecorderConfig = {
audioSampleRate : 22050, audioSampleRate : 22050,
numberOfChannels : 2, numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS, format : media.AudioOutputFormat.AAC_ADTS,
uri : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a', // The file must be created by the caller and granted with proper permissions. uri : testFdNumber, // testFdNumber is generated by getFd.
location : { latitude : 30, longitude : 130}, location : { latitude : 30, longitude : 130},
} }
audioRecorder.prepare(audioRecorderConfig); audioRecorder.prepare(audioRecorderConfig);
...@@ -92,6 +122,11 @@ audioRecorder = undefined; ...@@ -92,6 +122,11 @@ audioRecorder = undefined;
Unlike the full-process scenario, the normal recording scenario does not include the process of pausing and resuming recording. Unlike the full-process scenario, the normal recording scenario does not include the process of pausing and resuming recording.
```js ```js
import media from '@ohos.multimedia.media'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
let testFdNumber;
function SetCallBack(audioPlayer) { function SetCallBack(audioPlayer) {
audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. audioRecorder.on('prepare', () => { // Set the 'prepare' event callback.
console.log('prepare success'); console.log('prepare success');
...@@ -108,6 +143,32 @@ function SetCallBack(audioPlayer) { ...@@ -108,6 +143,32 @@ function SetCallBack(audioPlayer) {
console.log('audio recorder release success'); console.log('audio recorder release success');
}); });
} }
// pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Movies/01.mp3.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
async function getFd(pathName) {
let displayName = pathName;
const mediaTest = mediaLibrary.getMediaLibrary();
let fileKeyObj = mediaLibrary.FileKey;
let mediaType = mediaLibrary.MediaType.VIDEO;
let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
if (dataUri != undefined) {
let args = dataUri.id.toString();
let fetchOp = {
selections : fileKeyObj.ID + "=?",
selectionArgs : [args],
}
let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
let fileAsset = await fetchFileResult.getAllObject();
let fdNumber = await fileAsset[0].open('Rw');
fdNumber = "fd://" + fdNumber.toString();
testFdNumber = fdNumber;
}
}
await getFd('01.mp3');
// 1. Create an AudioRecorder instance. // 1. Create an AudioRecorder instance.
let audioRecorder = media.createAudioRecorder(); let audioRecorder = media.createAudioRecorder();
// 2. Set the callbacks. // 2. Set the callbacks.
...@@ -119,7 +180,7 @@ let audioRecorderConfig = { ...@@ -119,7 +180,7 @@ let audioRecorderConfig = {
audioSampleRate : 22050, audioSampleRate : 22050,
numberOfChannels : 2, numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS, format : media.AudioOutputFormat.AAC_ADTS,
uri : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a', // The file must be created by the caller and granted with proper permissions. uri : testFdNumber, // testFdNumber is generated by getFd.
location : { latitude : 30, longitude : 130}, location : { latitude : 30, longitude : 130},
} }
audioRecorder.prepare(audioRecorderConfig) audioRecorder.prepare(audioRecorderConfig)
......
...@@ -28,10 +28,23 @@ For details about the APIs used for video playback, see [js-apis-media.md](../re ...@@ -28,10 +28,23 @@ For details about the APIs used for video playback, see [js-apis-media.md](../re
The full video playback process includes creating an instance, setting the URL, setting the surface ID, preparing for video playback, playing video, pausing playback, obtaining track information, seeking to a playback position, setting the volume, setting the playback speed, stopping playback, resetting the playback configuration, and releasing resources. The full video playback process includes creating an instance, setting the URL, setting the surface ID, preparing for video playback, playing video, pausing playback, obtaining track information, seeking to a playback position, setting the volume, setting the playback speed, stopping playback, resetting the playback configuration, and releasing resources.
For details about the **url** media source input types supported by **VideoPlayer**, see the [url attribute](../reference/apis/js-apis-media.md#videoplayer_Attributes).
For details about how to create an Xcomponent, see [Xcomponent Creation](#Xcomponent).
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method. let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method.
let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface. let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
LoadXcomponent() {
surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
console.info('LoadXcomponent surfaceID is' + surfaceID);
}
// Report an error in the case of a function invocation failure. // Report an error in the case of a function invocation failure.
function failureCallback(error) { function failureCallback(error) {
console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Name is ${error.name}`);
...@@ -65,10 +78,19 @@ await media.createVideoPlayer().then((video) => { ...@@ -65,10 +78,19 @@ await media.createVideoPlayer().then((video) => {
} }
}, failureCallback).catch(catchCallback); }, failureCallback).catch(catchCallback);
// Set the URL of the video file selected by the user. // Set the FD (local playback) of the video file selected by the user.
videoPlayer.url = 'file:///data/data/ohos.xxx.xxx/files/test.mp4'; let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
// Call the Xcomponent interface to obtain the surface ID and save it to the surfaceID variable. videoPlayer.url = fdPath;
// Set the surface ID to display the video image. // Set the surface ID to display the video image.
await videoPlayer.setDisplaySurface(surfaceID).then(() => { await videoPlayer.setDisplaySurface(surfaceID).then(() => {
...@@ -145,9 +167,18 @@ surfaceID = undefined; ...@@ -145,9 +167,18 @@ surfaceID = undefined;
### Normal Playback Scenario ### Normal Playback Scenario
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method. let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method.
let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface. let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
LoadXcomponent() {
surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
console.info('LoadXcomponent surfaceID is' + surfaceID);
}
// Report an error in the case of a function invocation failure. // Report an error in the case of a function invocation failure.
function failureCallback(error) { function failureCallback(error) {
console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Name is ${error.name}`);
...@@ -189,10 +220,19 @@ await media.createVideoPlayer().then((video) => { ...@@ -189,10 +220,19 @@ await media.createVideoPlayer().then((video) => {
// Set the event callbacks. // Set the event callbacks.
SetCallBack(videoPlayer); SetCallBack(videoPlayer);
// Set the URL of the video file selected by the user. // Set the FD (local playback) of the video file selected by the user.
videoPlayer.url = 'file:///data/data/ohos.xxx.xxx/files/test.mp4'; let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
// Call the Xcomponent interface to obtain the surface ID and save it to the surfaceID variable. videoPlayer.url = fdPath;
// Set the surface ID to display the video image. // Set the surface ID to display the video image.
await videoPlayer.setDisplaySurface(surfaceID).then(() => { await videoPlayer.setDisplaySurface(surfaceID).then(() => {
...@@ -213,9 +253,18 @@ await videoPlayer.play().then(() => { ...@@ -213,9 +253,18 @@ await videoPlayer.play().then(() => {
### Switching to the Next Video Clip ### Switching to the Next Video Clip
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method. let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method.
let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface. let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
LoadXcomponent() {
surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
console.info('LoadXcomponent surfaceID is' + surfaceID);
}
// Report an error in the case of a function invocation failure. // Report an error in the case of a function invocation failure.
function failureCallback(error) { function failureCallback(error) {
console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Name is ${error.name}`);
...@@ -257,10 +306,19 @@ await media.createVideoPlayer().then((video) => { ...@@ -257,10 +306,19 @@ await media.createVideoPlayer().then((video) => {
// Set the event callbacks. // Set the event callbacks.
SetCallBack(videoPlayer); SetCallBack(videoPlayer);
// Set the URL of the video file selected by the user. // Set the FD (local playback) of the video file selected by the user.
videoPlayer.url = 'file:///data/data/ohos.xxx.xxx/files/test.mp4'; let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
// Call the Xcomponent interface to obtain the surface ID and save it to the surfaceID variable. videoPlayer.url = fdPath;
// Set the surface ID to display the video image. // Set the surface ID to display the video image.
await videoPlayer.setDisplaySurface(surfaceID).then(() => { await videoPlayer.setDisplaySurface(surfaceID).then(() => {
...@@ -283,7 +341,19 @@ await videoPlayer.reset().then(() => { ...@@ -283,7 +341,19 @@ await videoPlayer.reset().then(() => {
console.info('reset success'); console.info('reset success');
}, failureCallback).catch(catchCallback); }, failureCallback).catch(catchCallback);
videoPlayer.url = 'file:///data/data/ohos.xxx.xxx/files/next.mp4'; // Set the FD (local playback) of the video file selected by the user.
let fdNextPath = 'fd://'
let nextPath = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/02.mp4';
await fileIO.open(nextPath).then(fdNumber) => {
fdNextPath = fdNextPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdNextPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
videoPlayer.url = fdNextPath;
// Set the surface ID to display the video image. // Set the surface ID to display the video image.
await videoPlayer.setDisplaySurface(surfaceID).then(() => { await videoPlayer.setDisplaySurface(surfaceID).then(() => {
...@@ -304,9 +374,18 @@ await videoPlayer.play().then(() => { ...@@ -304,9 +374,18 @@ await videoPlayer.play().then(() => {
### Looping a Video Clip ### Looping a Video Clip
```js ```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method. let videoPlayer = undefined; // Used to store instances created by calling the createVideoPlayer method.
let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface. let surfaceID = undefined; // Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
LoadXcomponent() {
surfaceID = this.$element('Xcomponent').getXComponentSurfaceId();
console.info('LoadXcomponent surfaceID is' + surfaceID);
}
// Report an error in the case of a function invocation failure. // Report an error in the case of a function invocation failure.
function failureCallback(error) { function failureCallback(error) {
console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Name is ${error.name}`);
...@@ -348,10 +427,19 @@ await media.createVideoPlayer().then((video) => { ...@@ -348,10 +427,19 @@ await media.createVideoPlayer().then((video) => {
// Set the event callbacks. // Set the event callbacks.
SetCallBack(videoPlayer); SetCallBack(videoPlayer);
// Set the URL of the video file selected by the user. // Set the FD (local playback) of the video file selected by the user.
videoPlayer.url = 'file:///data/data/ohos.xxx.xxx/files/test.mp4'; let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
// Call the Xcomponent interface to obtain the surface ID and save it to the surfaceID variable. videoPlayer.url = fdPath;
// Set the surface ID to display the video image. // Set the surface ID to display the video image.
await videoPlayer.setDisplaySurface(surfaceID).then(() => { await videoPlayer.setDisplaySurface(surfaceID).then(() => {
...@@ -371,3 +459,15 @@ await videoPlayer.play().then(() => { ...@@ -371,3 +459,15 @@ await videoPlayer.play().then(() => {
console.info('play success'); console.info('play success');
}, failureCallback).catch(catchCallback); }, failureCallback).catch(catchCallback);
``` ```
### Xcomponent Creation
```js
The Xcomponent is used to obtain the surface ID during video playback. You need to create an xxx.hml file and add the following code to the xxx.hml file, where xxx is the same as that in the xxx.js file:
<xcomponent id = 'Xcomponent'
if = "{{isFlush}}" // Refresh the surface ID. To enable automatic loading of the Xcomponent and obtain the new surface ID, assign **false** to **isFlush** and then assign **true** to **isFlush**.
type = 'surface'
onload = 'LoadXcomponent' // Default interface for loading the Xcomponent.
style = "width:720px;height:480px;border-color:red;border-width:5px;"> // Set the window width, height, and other attributes.
</xcomponent>
```
...@@ -23,6 +23,36 @@ For details about the APIs used for video recording, see [js-apis-media.md](../r ...@@ -23,6 +23,36 @@ For details about the APIs used for video recording, see [js-apis-media.md](../r
The full video recording process includes creating an instance, setting recording parameters, recording video, pausing, resuming, and stopping recording, and releasing resources. The full video recording process includes creating an instance, setting recording parameters, recording video, pausing, resuming, and stopping recording, and releasing resources.
```js ```js
import media from '@ohos.multimedia.media'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
let testFdNumber;
// pathName indicates the passed recording file name, for example, 01.mp4. The generated file address is /storage/media/100/local/files/Movies/01.mp4.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
async function getFd(pathName) {
let displayName = pathName;
const mediaTest = mediaLibrary.getMediaLibrary();
let fileKeyObj = mediaLibrary.FileKey;
let mediaType = mediaLibrary.MediaType.VIDEO;
let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
if (dataUri != undefined) {
let args = dataUri.id.toString();
let fetchOp = {
selections : fileKeyObj.ID + "=?",
selectionArgs : [args],
}
let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
let fileAsset = await fetchFileResult.getAllObject();
let fdNumber = await fileAsset[0].open('Rw');
fdNumber = "fd://" + fdNumber.toString();
testFdNumber = fdNumber;
}
}
await getFd('01.mp4');
let videoProfile = { let videoProfile = {
audioBitrate : 48000, audioBitrate : 48000,
audioChannels : 2, audioChannels : 2,
...@@ -40,7 +70,7 @@ let videoConfig = { ...@@ -40,7 +70,7 @@ let videoConfig = {
audioSourceType : 1, audioSourceType : 1,
videoSourceType : 0, videoSourceType : 0,
profile : videoProfile, profile : videoProfile,
url : 'file:///data/media/01.mp4', url: testFdNumber, // testFdNumber is generated by getFd.
orientationHint : 0, orientationHint : 0,
location : { latitude : 30, longitude : 130 }, location : { latitude : 30, longitude : 130 },
} }
...@@ -61,6 +91,7 @@ function catchCallback(error) { ...@@ -61,6 +91,7 @@ function catchCallback(error) {
let videoRecorder = null; // videoRecorder is an empty object and assigned with a value after createVideoRecorder is successfully called. let videoRecorder = null; // videoRecorder is an empty object and assigned with a value after createVideoRecorder is successfully called.
let surfaceID = null; // Used to save the surface ID returned by getInputSurface. let surfaceID = null; // Used to save the surface ID returned by getInputSurface.
// Create a VideoRecorder object. // Create a VideoRecorder object.
await media.createVideoRecorder().then((recorder) => { await media.createVideoRecorder().then((recorder) => {
console.info('case createVideoRecorder called'); console.info('case createVideoRecorder called');
......
# Media # Media
> **NOTE**
> 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. The multimedia subsystem provides a set of simple and easy-to-use APIs for you to access the system and use media resources.
This subsystem offers various media services covering audio and video, which provide the following capabilities: This subsystem offers various media services covering audio and video, which provide the following capabilities:
- Audio playback ([AudioPlayer](#audioplayer)) - Audio playback ([AudioPlayer](#audioplayer))
- Video playback ([VideoPlayer](#videoplayer8))
- Audio recording ([AudioRecorder](#audiorecorder)) - Audio recording ([AudioRecorder](#audiorecorder))
- Video recording ([VideoRecorder](#VideoRecorder<sup>8+</sup>))
The following capabilities will be provided in the future: video playback, video recording, data source audio/video playback, audio/video encoding and decoding, container encapsulation and decapsulation, and media capability query. The following capabilities will be provided in later versions: data source audio/video playback, audio/video encoding and decoding, container encapsulation and decapsulation, and media capability query.
## Modules to Import ## Modules to Import
...@@ -21,179 +26,260 @@ createAudioPlayer(): [AudioPlayer](#audioplayer) ...@@ -21,179 +26,260 @@ createAudioPlayer(): [AudioPlayer](#audioplayer)
Creates an **AudioPlayer** instance in synchronous mode. Creates an **AudioPlayer** instance in synchronous mode.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Return value** **Return value**
| Type| Description| | Type | Description |
| --------------------------- | ------------------------------------------------------------ | | --------------------------- | ------------------------------------------------------------ |
| [AudioPlayer](#audioplayer) | Returns the **AudioPlayer** instance if the operation is successful; returns **null** otherwise. After the instance is created, you can start, pause, or stop audio playback.| | [AudioPlayer](#audioplayer) | Returns the **AudioPlayer** instance if the operation is successful; returns **null** otherwise. After the instance is created, you can start, pause, or stop audio playback.|
**Example** **Example**
```js ```js
var audioPlayer = media.createAudioPlayer(); let audioPlayer = media.createAudioPlayer();
``` ```
## media.createAudioPlayerAsync<sup>8+</sup> ## media.createVideoPlayer<sup>8+</sup>
createAudioPlayerAsync(callback: AsyncCallback\<[AudioPlayer](#audioplayer)>): void createVideoPlayer(callback: AsyncCallback\<[VideoPlayer](#videoplayer8)>): void
Creates an **AudioPlayer** instance in asynchronous mode. This method uses a callback to return the result. Creates a **VideoPlayer** instance in asynchronous mode. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | ------------------------------------------ | ---- | ------------------------------ | | -------- | ------------------------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback<[AudioPlayer](#audioplayer)> | Yes| Callback used to return the **AudioPlayer** instance created.| | callback | AsyncCallback<[VideoPlayer](#videoplayer8)> | Yes | Callback used to return the **VideoPlayer** instance created.|
**Example** **Example**
```js ```js
media.createAudioPlayerAsync((error, audio) => { let videoPlayer
if (typeof(audio) != 'undefined') {
audioPlayer = audio; media.createVideoPlayer((error, video) => {
console.info('audio createAudioPlayerAsync success'); if (typeof(video) != 'undefined') {
videoPlayer = video;
console.info('video createVideoPlayer success');
} else { } else {
console.info(`audio createAudioPlayerAsync fail, error:${error.message}`); console.info(`video createVideoPlayer fail, error:${error.message}`);
} }
}); });
``` ```
## media.createAudioPlayerAsync<sup>8+</sup> ## media.createVideoPlayer<sup>8+</sup>
createAudioPlayerAsync: Promise<[AudioPlayer](#audioplayer)> createVideoPlayer: Promise<[VideoPlayer](#videoplayer8)>
Creates an **AudioPlayer** instance in asynchronous mode. This method uses a promise to return the result. Creates a **VideoPlayer** instance in asynchronous mode. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value** **Return value**
| Type| Description| | Type | Description |
| ------------------------------------ | ----------------------------------- | | ------------------------------------- | ----------------------------------- |
| Promise<[AudioPlayer](#audioplayer)> | Promise used to return the **AudioPlayer** instance created.| | Promise<[VideoPlayer](#videoplayer8)> | Promise used to return the **VideoPlayer** instance created.|
**Example** **Example**
```js ```js
let videoPlayer
function failureCallback(error) { function failureCallback(error) {
console.info(`audio failureCallback, error:${error.message}`); console.info(`video failureCallback, error:${error.message}`);
} }
function catchCallback(error) { function catchCallback(error) {
console.info(`audio catchCallback, error:${error.message}`); console.info(`video catchCallback, error:${error.message}`);
} }
await media.createAudioPlayerAsync.then((audio) => { await media.createVideoPlayer.then((video) => {
if (typeof(audio) != 'undefined') { if (typeof(video) != 'undefined') {
audioPlayer = audio; videoPlayer = video;
console.info('audio createAudioPlayerAsync success'); console.info('video createVideoPlayer success');
} else { } else {
console.info('audio createAudioPlayerAsync fail'); console.info('video createVideoPlayer fail');
} }
}, failureCallback).catch(catchCallback); }, failureCallback).catch(catchCallback);
``` ```
## media.createAudioRecorder ## media.createAudioRecorder
createAudioRecorder(): AudioRecorder createAudioRecorder(): AudioRecorder
Creates an **AudioRecorder** instance to control audio recording. Creates an **AudioRecorder** instance to control audio recording.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Return value** **Return value**
| Type| Description| | Type | Description |
| ------------------------------- | ----------------------------------------- | | ------------------------------- | ----------------------------------------- |
| [AudioRecorder](#audiorecorder) | Returns the **AudioRecorder** instance if the operation is successful; returns **null** otherwise.| | [AudioRecorder](#audiorecorder) | Returns the **AudioRecorder** instance if the operation is successful; returns **null** otherwise.|
**Example** **Example**
```js
let audiorecorder = media.createAudioRecorder();
```
## media.createVideoRecorder<sup>8+</sup>
createVideoRecorder(callback: AsyncCallback\<[VideoRecorder](#videorecorder8)>): void
Creates a **VideoRecorder** instance in asynchronous mode. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------------------------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback<[VideoRecorder](#videorecorder8)> | Yes | Callback used to return the **VideoRecorder** instance created.|
**Example**
```js
let videoRecorder
media.createVideoRecorder((error, video) => {
if (typeof(video) != 'undefined') {
videoRecorder = video;
console.info('video createVideoRecorder success');
} else {
console.info(`video createVideoRecorder fail, error:${error.message}`);
}
});
``` ```
var audiorecorder = media.createAudioRecorder();
## media.createVideoRecorder<sup>8+</sup>
createVideoRecorder: Promise<[VideoRecorder](#videorecorder8)>
Creates a **VideoRecorder** instance in asynchronous mode. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| ----------------------------------------- | ----------------------------------- |
| Promise<[VideoRecorder](#videorecorder8)> | Promise used to return the **VideoRecorder** instance created.|
**Example**
```js
let videoRecorder
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await media.createVideoRecorder.then((video) => {
if (typeof(video) != 'undefined') {
videoRecorder = video;
console.info('video createVideoRecorder success');
} else {
console.info('video createVideoRecorder fail');
}
}, failureCallback).catch(catchCallback);
``` ```
## MediaErrorCode<sup>8+</sup> ## MediaErrorCode<sup>8+</sup>
Enumerates the media error codes. Enumerates the media error codes.
| Name| Value| Description| | Name | Value | Description |
| -------------------------- | ---- | -------------------------------------- | | -------------------------- | ---- | ------------------------------------------------------------ |
| MSERR_OK | 0 | The operation is successful.| | MSERR_OK | 0 | The operation is successful.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_NO_MEMORY | 1 | Failed to allocate memory. The system may have no available memory.| | MSERR_NO_MEMORY | 1 | Failed to allocate memory. The system may have no available memory.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_OPERATION_NOT_PERMIT | 2 | No permission to perform this operation.| | MSERR_OPERATION_NOT_PERMIT | 2 | No permission to perform this operation.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_INVALID_VAL | 3 | Invalid input parameter.| | MSERR_INVALID_VAL | 3 | Invalid input parameter.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_IO | 4 | An I/O error occurs.| | MSERR_IO | 4 | An I/O error occurs.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_TIMEOUT | 5 | The operation times out.| | MSERR_TIMEOUT | 5 | The operation times out.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_UNKNOWN | 6 | An unknown error occurs.| | MSERR_UNKNOWN | 6 | An unknown error occurs.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_SERVICE_DIED | 7 | Invalid server.| | MSERR_SERVICE_DIED | 7 | Invalid server.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_INVALID_STATE | 8 | The operation is not allowed in the current state.| | MSERR_INVALID_STATE | 8 | The operation is not allowed in the current state.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MSERR_UNSUPPORTED | 9 | The operation is not supported in the current version.| | MSERR_UNSUPPORTED | 9 | The operation is not supported in the current version.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
## MediaType<sup>8+</sup> ## MediaType<sup>8+</sup>
Enumerates the media types. Enumerates the media types.
| Name| Value| Description| | Name | Value | Description |
| ------------------- | ---- | ------------------ | | -------------- | ---- | ------------------------------------------------------------ |
| MEDIA_TYPE_AUD | 0 | Media.| | MEDIA_TYPE_AUD | 0 | Media.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MEDIA_TYPE_VID | 1 | Video.| | MEDIA_TYPE_VID | 1 | Video.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MEDIA_TYPE_SUBTITLE | 2 | Subtitle. (under development)|
## CodecMimeType<sup>8+</sup> ## CodecMimeType<sup>8+</sup>
Enumerates the codec MIME types. Enumerates the codec MIME types.
| Name| Value| Description| | Name | Value | Description |
| ------------ | ----------------- | ------------------------ | | ------------ | ----------------- | ------------------------------------------------------------ |
| AUDIO_MPEG | "audio/mpeg" | Audio in MPEG format.| | VIDEO_MPEG4 | "video/mp4v-es" | Video in MPEG-4 format.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| AUDIO_AAC | "audio/mp4a-latm" | Audio in MP4A-LATM format.| | AUDIO_AAC | "audio/mp4a-latm" | Audio in MP4A-LATM format.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| AUDIO_VORBIS | "audio/vorbis" | Audio in Vorbis format.| | AUDIO_VORBIS | "audio/vorbis" | Audio in Vorbis format.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| AUDIO_FLAC | "audio/flac" | Audio in FLAC format.| | AUDIO_FLAC | "audio/flac" | Audio in FLAC format.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
## MediaDescriptionKey<sup>8+</sup> ## MediaDescriptionKey<sup>8+</sup>
Enumerates the media description keys. Enumerates the media description keys.
| Name| Value| Description| | Name | Value | Description |
| ------------------------ | --------------- | ------------------------------------------------------------ | | ------------------------ | --------------- | ------------------------------------------------------------ |
| MD_KEY_TRACK_INDEX | "track_index" | Track index, which is a number.| | MD_KEY_TRACK_INDEX | "track_index" | Track index, which is a number.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_TRACK_TYPE | "track_type" | Track type, which is a number. For details, see [MediaType](#mediatype8).| | MD_KEY_TRACK_TYPE | "track_type" | Track type, which is a number. For details, see [MediaType](#mediatype8).<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_CODEC_MIME | "codec_mime" | Codec MIME type, which is a string.| | MD_KEY_CODEC_MIME | "codec_mime" | Codec MIME type, which is a string.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_DURATION | "duration" | Media duration, which is a number, in units of ms.| | MD_KEY_DURATION | "duration" | Media duration, which is a number, in units of ms.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_BITRATE | "bitrate" | Bit rate, which is a number, in units of bit/s.| | MD_KEY_BITRATE | "bitrate" | Bit rate, which is a number, in units of bit/s.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_WIDTH | "width" | Video width, which is a number, in units of pixel.| | MD_KEY_WIDTH | "width" | Video width, which is a number, in units of pixel.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_HEIGHT | "height" | Video height, which is a number, in units of pixel.| | MD_KEY_HEIGHT | "height" | Video height, which is a number, in units of pixel.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_FRAME_RATE | "frame_rate" | Video frame rate, which is a number, in units of 100 fps.| | MD_KEY_FRAME_RATE | "frame_rate" | Video frame rate, which is a number, in units of 100 fps.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_AUD_CHANNEL_COUNT | "channel_count" | Number of audio channels, which is a number.| | MD_KEY_AUD_CHANNEL_COUNT | "channel_count" | Number of audio channels, which is a number.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| MD_KEY_AUD_SAMPLE_RATE | "sample_rate" | Sampling rate, which is a number, in units of Hz.| | MD_KEY_AUD_SAMPLE_RATE | "sample_rate" | Sampling rate, which is a number, in units of Hz.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
## BufferingInfoType<sup>8+</sup> ## BufferingInfoType<sup>8+</sup>
Enumerates the buffering event types. Enumerates the buffering event types.
| Name| Value| Description| | Name | Value | Description |
| ----------------- | ---- | -------------------------- | | ----------------- | ---- | ------------------------------------------------------------ |
| BUFFERING_START | 1 | Buffering starts.| | BUFFERING_START | 1 | Buffering starts.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| BUFFERING_END | 2 | Buffering ends.| | BUFFERING_END | 2 | Buffering ends.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| BUFFERING_PERCENT | 3 | Buffering progress, in percent.| | BUFFERING_PERCENT | 3 | Buffering progress, in percent.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| CACHED_DURATION | 4 | Cache duration, in milliseconds.| | CACHED_DURATION | 4 | Cache duration, in milliseconds.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
## AudioPlayer ## AudioPlayer
Manages and plays audio. Before calling a method of the **AudioPlayer** class, you must call [createAudioPlayer()](#media.createaudioplayer) or [createAudioPlayerAsync()](#media.createaudioplayerasync8) to create an [AudioPlayer](#audioplayer) instance. Provides methods to manage and play audio. Before calling a method of **AudioPlayer**, you must use [createAudioPlayer()](#mediacreateaudioplayer) to create an **AudioPlayer** instance.
For details about the audio playback demo, see [Audio Playback Development](.../media/audio-playback.md). For details about the audio playback demo, see [Audio Playback Development](../../media/audio-playback.md).
### Attributes<a name=audioplayer_Attributes></a> ### Attributes<a name=audioplayer_Attributes></a>
| Name| Type| Readable| Writable| Description| | Name | Type | Readable| Writable| Description |
| ----------- | ------------------------- | ---- | ---- | ------------------------------------------------------------ | | ----------- | ------------------------- | ---- | ---- | ------------------------------------------------------------ |
| src | string | Yes| Yes| Audio media URI. The mainstream audio formats (MP4, AAC, MP3, and OGG) are supported. <br>**Examples of supported URIs**:<br>1. Local absolute path: file:///data/data/ohos.xxx.xxx/files/test.mp4<br>![en-us_image_0000001164217678](figures/en-us_image_0000001164217678.png)<br>2. HTTP network playback path (under development)<br>3. HLS network playback path (under development)<br>4. FD playback (under development)<br>**Precautions**: <br>Media files can be played only after the read permission is granted.| | src | string | Yes | Yes | Audio media URI. The mainstream audio formats (MP4, AAC, MP3, and OGG) are supported.<br>**Example of supported URIs**:<br>1. FD playback: fd://xxx<br>![en-us_image_0000001164217678](figures/en-us_image_url.png)<br>2. HTTP network playback path (under development)<br>3. HLS network playback path (under development)<br>**Note**:<br>To use media materials, you must declare the read permission. Otherwise, the media materials cannot be played properly.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| loop | boolean | Yes| Yes| Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite.| | loop | boolean | Yes | Yes | Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| currentTime | number | Yes| No| Current audio playback position.| | currentTime | number | Yes | No | Current audio playback position.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| duration | number | Yes| No| Audio duration.| | duration | number | Yes | No | Audio duration.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| state | [AudioState](#audiostate) | Yes| No| Audio playback state.| | state | [AudioState](#audiostate) | Yes | No | Audio playback state.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
### play<a name=audioplayer_play></a> ### play<a name=audioplayer_play></a>
play(): void play(): void
Starts to play audio resources. This method can be called only after the **dataLoad** event is triggered. Starts to play audio resources. This method can be called only after the [dataLoad](#on('play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange')) event is triggered.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Example** **Example**
...@@ -210,6 +296,8 @@ pause(): void ...@@ -210,6 +296,8 @@ pause(): void
Pauses audio playback. Pauses audio playback.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Example** **Example**
```js ```js
...@@ -225,6 +313,8 @@ stop(): void ...@@ -225,6 +313,8 @@ stop(): void
Stops audio playback. Stops audio playback.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Example** **Example**
```js ```js
...@@ -240,6 +330,8 @@ reset(): void ...@@ -240,6 +330,8 @@ reset(): void
Switches the audio resource to be played. Switches the audio resource to be played.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Example** **Example**
```js ```js
...@@ -255,11 +347,13 @@ seek(timeMs: number): void ...@@ -255,11 +347,13 @@ seek(timeMs: number): void
Seeks to the specified playback position. Seeks to the specified playback position.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------ | | ------ | ------ | ---- | ------------------------------ |
| timeMs | number | Yes| Position to seek to, in milliseconds.| | timeMs | number | Yes | Position to seek to, in milliseconds.|
**Example** **Example**
...@@ -280,11 +374,13 @@ setVolume(vol: number): void ...@@ -280,11 +374,13 @@ setVolume(vol: number): void
Sets the volume. Sets the volume.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ | | ------ | ------ | ---- | ------------------------------------------------------------ |
| vol | number | Yes| Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).| | vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).|
**Example** **Example**
...@@ -299,7 +395,9 @@ audioPlayer.setVolume(1); // Set the volume to 100%. ...@@ -299,7 +395,9 @@ audioPlayer.setVolume(1); // Set the volume to 100%.
release(): void release(): void
Releases this **AudioPlayer** instance. Releases the audio playback resource.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Example** **Example**
...@@ -312,13 +410,15 @@ audioPlayer = undefined; ...@@ -312,13 +410,15 @@ audioPlayer = undefined;
getTrackDescription(callback: AsyncCallback<Array<[MediaDescription](#mediadescription8)>>): void getTrackDescription(callback: AsyncCallback<Array<[MediaDescription](#mediadescription8)>>): void
Obtains the audio track information. This method uses a callback to return the result. Obtains the audio track information. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | ------------------------------------------------------------ | ---- | -------------------------- | | -------- | ------------------------------------------------------------ | ---- | -------------------------- |
| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes| Callback used to return the audio track information obtained.| | callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return the audio track information obtained.|
**Example** **Example**
...@@ -346,11 +446,13 @@ audioPlayer.getTrackDescription((error, arrlist) => { ...@@ -346,11 +446,13 @@ audioPlayer.getTrackDescription((error, arrlist) => {
getTrackDescription(): Promise<Array<[MediaDescription](#mediadescription8)>> getTrackDescription(): Promise<Array<[MediaDescription](#mediadescription8)>>
Obtains the audio track information. This method uses a promise to return the result. Obtains the audio track information. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Return value** **Return value**
| Type| Description| | Type | Description |
| ------------------------------------------------------ | ------------------------------- | | ------------------------------------------------------ | ------------------------------- |
| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return the audio track information obtained.| | Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return the audio track information obtained.|
...@@ -389,12 +491,14 @@ on(type: 'bufferingUpdate', callback: (infoType: [BufferingInfoType](#bufferingi ...@@ -389,12 +491,14 @@ on(type: 'bufferingUpdate', callback: (infoType: [BufferingInfoType](#bufferingi
Subscribes to the audio buffering update event. Subscribes to the audio buffering update event.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | | -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type | string | Yes| Type of the event to subscribe to, which is 'bufferingUpdate' in this example.| | type | string | Yes | Type of the event to subscribe to, which is 'bufferingUpdate' in this example. |
| callback | (infoType: [BufferingInfoType](#bufferinginfotype8), value: number) => void | Yes| Callback invoked when the event is triggered. <br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.| | callback | (infoType: [BufferingInfoType](#bufferinginfotype8), value: number) => void | Yes | Callback invoked when the event is triggered.<br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.|
**Example** **Example**
...@@ -411,12 +515,14 @@ on(type: 'play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeCh ...@@ -411,12 +515,14 @@ on(type: 'play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeCh
Subscribes to the audio playback events. Subscribes to the audio playback events.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | ---------- | ---- | ------------------------------------------------------------ | | -------- | ---------- | ---- | ------------------------------------------------------------ |
| type | string | Yes| Type of the event to subscribe to. The following events are supported: 'play' \| 'pause' \| 'stop' \| 'reset' \| 'dataLoad' \| 'finish' \| 'volumeChange' <br>- The 'play' event is triggered when the [play()](#play) method is called and audio playback starts. <br>- The 'pause' event is triggered when the [pause()](#pause) method is called and audio playback is paused. <br>- The 'stop' event is triggered when the [stop()](#stop) method is called and audio playback stops. <br>- The 'reset' event is triggered when the [reset()](#reset7) method is called and audio playback is reset. <br>- The 'dataLoad' event is triggered when the audio data is loaded, that is, when the **src** attribute is configured. <br>- The 'finish' event is triggered when the audio playback is finished. <br>- The 'volumeChange' event is triggered when the [setVolume()](#setvolume) method is called and the playback volume is changed.| | type | string | Yes | Type of the event to subscribe to. The following events are supported: 'play' \| 'pause' \| 'stop' \| 'reset' \| 'dataLoad' \| 'finish' \| 'volumeChange'<br>- The 'play' event is triggered when the [play()](#play) method is called and audio playback starts.<br>- The 'pause' event is triggered when the [pause()](#pause) method is called and audio playback is paused.<br>- The 'stop' event is triggered when the [stop()](#stop) method is called and audio playback stops.<br>- The 'reset' event is triggered when the [reset()](#reset7) method is called and audio playback is reset.<br>- The 'dataLoad' event is triggered when the audio data is loaded, that is, when the **src** attribute is configured.<br>- The 'finish' event is triggered when the audio playback is finished.<br>- The 'volumeChange' event is triggered when the [setVolume()](#setvolume) method is called and the playback volume is changed.|
| callback | () => void | Yes| Callback invoked when the event is triggered.| | callback | () => void | Yes | Callback invoked when the event is triggered. |
**Example** **Example**
...@@ -460,21 +566,35 @@ audioPlayer.on('error', (error) => { // Set the 'error' event callback ...@@ -460,21 +566,35 @@ audioPlayer.on('error', (error) => { // Set the 'error' event callback
console.info(`audio error called, errCode is ${error.code}`); console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`); console.info(`audio error called, errMessage is ${error.message}`);
}); });
audioPlayer.src = 'file:///data/data/ohos.xxx.xxx/files/test.mp4'; // Set the src attribute and trigger the 'dataLoad' event callback.
// Set the FD (local playback) of the video file selected by the user.
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback.
``` ```
### on('timeUpdate') ### on('timeUpdate')
on(type: 'timeUpdate', callback: Callback\<number>): void on(type: 'timeUpdate', callback: Callback\<number>): void
Subscribes to the [seek()](#seek) event. Subscribes to the 'timeUpdate' event.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | ----------------- | ---- | ------------------------------------------------------------ | | -------- | ----------------- | ---- | ------------------------------------------------------------ |
| type | string | Yes| Type of the event to subscribe to, which is 'timeUpdate' in this method. <br>The 'timeUpdate' event is triggered when the [seek()](#seek) method is called.| | type | string | Yes | Type of the event to subscribe to, which is 'timeUpdate' in this method.<br>The 'timeUpdate' event is triggered when the [seek()](#seek) method is called.|
| callback | Callback\<number> | Yes| Callback invoked when the event is triggered. The input parameter of the callback is the time when the seek operation is successful.| | callback | Callback\<number> | Yes | Callback invoked when the event is triggered. The input parameter of the callback is the time when the seek operation is successful. |
**Example** **Example**
...@@ -495,12 +615,14 @@ on(type: 'error', callback: ErrorCallback): void ...@@ -495,12 +615,14 @@ on(type: 'error', callback: ErrorCallback): void
Subscribes to the audio playback error event. Subscribes to the audio playback error event.
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ | | -------- | ------------- | ---- | ------------------------------------------------------------ |
| type | string | Yes| Type of the event to subscribe to, which is 'error' in this method. <br>The 'error' event is triggered when an error occurs during audio playback.| | type | string | Yes | Type of the event to subscribe to, which is 'error' in this method.<br>The 'error' event is triggered when an error occurs during audio playback.|
| callback | ErrorCallback | Yes| Callback invoked when the event is triggered.| | callback | ErrorCallback | Yes | Callback invoked when the event is triggered. |
**Example** **Example**
...@@ -515,198 +637,1820 @@ audioPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'erro ...@@ -515,198 +637,1820 @@ audioPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'erro
## AudioState ## AudioState
Describes the audio playback state. You can obtain the state through the **state** attribute. Enumerates the audio playback states. You can obtain the state through the **state** attribute.
| Name| Type| Description| | Name | Type | Description |
| ------------------ | ------ | -------------- | | ------------------ | ------ | ------------------------------------------------------------ |
| idle | string | Audio playback is idle.| | idle | string | The audio player is idle.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| playing | string | Audio playback is in progress.| | playing | string | Audio playback is in progress.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| paused | string | Audio playback is paused.| | paused | string | Audio playback is paused.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| stopped | string | Audio playback is stopped.| | stopped | string | Audio playback is stopped.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
| error<sup>8+</sup> | string | Audio playback is in the error state.| | error<sup>8+</sup> | string | Audio playback is in the error state.<br>**System capability**: SystemCapability.Multimedia.Media.AudioPlayer|
## MediaDescription<sup>8+</sup> ## VideoPlayer<sup>8+</sup>
### [key : string] : any Provides methods to manage and play video. Before calling a method of the **VideoPlayer** class, you must call [createVideoPlayer()](#media.createvideoplayer8) to create a [VideoPlayer](#videoplayer8) instance.
Defines media information in key-value mode. For details about the video playback demo, see [Video Playback Development](../../media/video-playback.md).
| Name| Type| Description| ### Attributes<a name=videoplayer_attributes></a>
| ----- | ------ | ------------------------------------------------------------ |
| key | string | Key of the media information. For details about the keys, see [MediaDescriptionKey](#mediadescriptionkey8).| | Name | Type | Readable| Writable| Description |
| value | any | Value of the key. For details about the values, see [MediaDescriptionKey](#mediadescriptionkey8).| | ------------------------ | ---------------------------------- | ---- | ---- | ------------------------------------------------------------ |
| 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 playback: fd://xxx<br>![en-us_image_0000001164217678](figures/en-us_image_url.png)<br>**Note**:<br>To use media materials, you must declare the read permission. Otherwise, the media materials cannot be played properly.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| 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.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| currentTime<sup>8+</sup> | number | Yes | No | Current video playback position.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| duration<sup>8+</sup> | number | Yes | No | Video duration. The value **-1** indicates the live streaming mode.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| state<sup>8+</sup> | [VideoPlayState](#videoplaystate8) | Yes | No | Video playback state.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| width<sup>8+</sup> | number | Yes | No | Video width.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| height<sup>8+</sup> | number | Yes | No | Video height.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
### setDisplaySurface<sup>8+</sup>
setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void
Sets **SurfaceId**. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | -------- | ---- | ------------------------- |
| surfaceId | string | Yes | Surface ID to set. |
| callback | function | Yes | Callback used to set **SurfaceId**.|
**Example** **Example**
```js ```js
function printfItemDescription(obj, key) { videoPlayer.setDisplaySurface(surfaceId, (err) => {
let property = obj[key]; if (typeof (err) == 'undefined') {
console.info('audio key is ' + key); console.info('setDisplaySurface success!');
console.info('audio value is ' + property);
}
audioPlayer.getTrackDescription((error, arrlist) => {
if (typeof (arrlist) != 'undefined') {
for (let i = 0; i < arrlist.length; i++) {
printfItemDescription(arrlist[i], MD_KEY_TRACK_TYPE); // Print the MD_KEY_TRACK_TYPE value of each track.
}
} else { } else {
console.log(`audio getTrackDescription fail, error:${error.message}`); console.info('setDisplaySurface fail!');
} }
}); });
``` ```
## AudioRecorder ### setDisplaySurface<sup>8+</sup>
Implements audio recording. Before calling a method of the **AudioRecorder** class, you must call [createAudioRecorder()](#mediacreateaudiorecorder) to create an **AudioRecorder** instance.
### prepare setDisplaySurface(surfaceId: string): Promise\<void>
prepare(config: AudioRecorderConfig): void Sets **SurfaceId**. This API uses a promise to return the result.
Prepares for recording. **System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | | --------- | ------ | ---- | --------- |
| config | [AudioRecorderConfig](#audiorecorderconfig) | Yes| Recording parameters, including the audio output URI, encoding format, sampling rate, and number of audio channels.| | surfaceId | string | Yes | Surface ID to set.|
**Return value**
| Type | Description |
| ------------- | ------------------------------ |
| Promise<void> | Promise used to set **SurfaceId**.|
**Example** **Example**
``` ```js
var audiorecorder = media.createAudioRecorder(); function failureCallback(error) {
let audioRecorderConfig = { console.info(`video failureCallback, error:${error.message}`);
audioEncoder : AAC_LC , }
audioEncodeBitRate : 22050, function catchCallback(error) {
audioSampleRate : 22050, console.info(`video catchCallback, error:${error.message}`);
numberOfChannels : 2,
format : AAC_ADTS,
uri : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a',
} }
audiorecorder.prepare(audioRecorderConfig) await videoPlayer.setDisplaySurface(surfaceId).then(() => {
console.info('setDisplaySurface success');
}, failureCallback).catch(catchCallback);
``` ```
### prepare<sup>8+</sup>
### start prepare(callback: AsyncCallback\<void>): void
start(): void Prepares for video playback. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Starts audio recording. **Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes | Callback used to return the result.|
**Example** **Example**
```js
videoPlayer.prepare((err) => {
if (typeof (err) == 'undefined') {
console.info('prepare success!');
} else {
console.info('prepare fail!');
}
});
``` ```
var audiorecorder = media.createAudioRecorder();
audiorecorder.start(); ### prepare<sup>8+</sup>
prepare(): Promise\<void>
Prepares for video playback. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value**
| Type | Description |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.prepare().then(() => {
console.info('prepare success');
}, failureCallback).catch(catchCallback);
``` ```
### stop ### play<sup>8+</sup>
stop(): void play(callback: AsyncCallback\<void>): void;
Stops audio recording. Starts to play video resources. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes | Callback used to return the result.|
**Example** **Example**
```js
videoPlayer.play((err) => {
if (typeof (err) == 'undefined') {
console.info('play success!');
} else {
console.info('play fail!');
}
});
``` ```
var audiorecorder = media.createAudioRecorder();
audiorecorder.stop(); ### play<sup>8+</sup>
play(): Promise\<void>;
Starts to play video resources. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value**
| Type | Description |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.play().then(() => {
console.info('play success');
}, failureCallback).catch(catchCallback);
``` ```
### release ### pause<sup>8+</sup>
release(): void pause(callback: AsyncCallback\<void>): void
Pauses video playback. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
Releases this **AudioRecorder** instance. | Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes | Callback used to return the result.|
**Example** **Example**
```js
videoPlayer.pause((err) => {
if (typeof (err) == 'undefined') {
console.info('pause success!');
} else {
console.info('pause fail!');
}
});
``` ```
var audiorecorder = media.createAudioRecorder();
audiorecorder.release(); ### pause<sup>8+</sup>
pause(): Promise\<void>
Pauses video playback. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value**
| Type | Description |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.pause().then(() => {
console.info('pause success');
}, failureCallback).catch(catchCallback);
``` ```
### reset ### stop<sup>8+</sup>
reset(): void stop(callback: AsyncCallback\<void>): void
Resets audio recording. Stops video playback. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Before resetting audio recording, you must call **stop()** to stop recording. After audio recording is reset, you must call **prepare()** to set the recording configurations for another recording. **Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes | Callback used to return the result.|
**Example** **Example**
```js
videoPlayer.stop((err) => {
if (typeof (err) == 'undefined') {
console.info('stop success!');
} else {
console.info('stop fail!');
}
});
``` ```
var audiorecorder = media.createAudioRecorder();
audiorecorder.reset(); ### stop<sup>8+</sup>
stop(): Promise\<void>
Stops video playback. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value**
| Type | Description |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.stop().then(() => {
console.info('stop success');
}, failureCallback).catch(catchCallback);
``` ```
### on('prepare' | 'start' | 'stop' | 'release' | 'reset') ### reset<sup>8+</sup>
on(type: 'prepare' | 'start' | 'stop' | 'release' | 'reset', callback: () => void): void reset(callback: AsyncCallback\<void>): void
Subscribes to the audio recording events. Switches the video resource to be played. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ | | -------- | -------- | ---- | ------------------------ |
| type | string | Yes| Type of the event to subscribe to. The following events are supported: 'prepare'\|'start'\|'stop'\|'release'\|'reset' <br/>- The 'prepare' event is triggered when audio recording preparation is complete. <br/>- The 'start' event is triggered when audio recording starts. <br/>- The 'stop' event is triggered when audio recording is stopped. <br/>- The 'release' event is triggered when the resources related to audio recording are released. <br/>- The 'reset' event is triggered when audio recording is reset.| | callback | function | Yes | Callback used to return the result.|
| callback | function | Yes| Callback invoked when the event is triggered.|
**Example** **Example**
``` ```js
var audiorecorder = media.createAudioRecorder(); videoPlayer.reset((err) => {
audiorecorder.on('prepare', () => { if (typeof (err) == 'undefined') {
console.log('Preparation succeeded.'); console.info('reset success!');
audiorecorder.start(); } else {
console.info('reset fail!');
}
}); });
``` ```
### on('error') ### reset<sup>8+</sup>
on(type: 'error', callback: ErrorCallback): void reset(): Promise\<void>
Subscribes to the audio recording error event. Switches the video resource to be played. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value**
| Type | Description |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.reset().then(() => {
console.info('reset success');
}, failureCallback).catch(catchCallback);
```
### seek<sup>8+</sup>
seek(timeMs: number, callback: AsyncCallback\<number>): void
Seeks to the specified playback position. The next key frame at the specified position is played. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ | | -------- | -------- | ---- | ------------------------------ |
| type | string | Yes| Type of the event to subscribe to, which is 'error' in this method. <br/>The 'error' event is triggered when an error occurs during audio recording.| | timeMs | number | Yes | Position to seek to, in milliseconds.|
| callback | ErrorCallback | Yes| Callback invoked when the event is triggered.| | callback | function | Yes | Callback used to return the result.|
**Example**
## AudioRecorderConfig ```js
videoPlayer.seek((seekTime, err) => {
if (typeof (err) == 'undefined') {
console.info('seek success!');
} else {
console.info('seek fail!');
}
});
```
Describes audio recording configurations. ### seek<sup>8+</sup>
| Name| Type| Mandatory| Description| seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void
| ------------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| audioEncoder | [AudioEncoder](#audioencoder) | No| Audio encoding format. The default value is **AAC_LC**.|
| audioEncodeBitRate | number | No| Audio encoding bit rate. The default value is **48000**.|
| audioSampleRate | number | No| Audio sampling rate. The default value is **48000**.|
| numberOfChannels | number | No| Number of audio channels. The default value is **2**.|
| format | [AudioOutputFormat](#audiooutputformat) | No| Audio output format. The default value is **MPEG_4**.|
| uri | string | Yes| Audio output URI. The following URI types are supported:<br/> 1. Absolute file path: file:///data/data/ohos.xxx.xxx/cache/test.mp4![en-us_image_0000001164217678](figures/en-us_image_0000001164217678.png)<br/>2. FD path: file://1&nbsp;(fd&nbsp;number)|
Seeks to the specified playback position. This API uses a callback to return the result.
## AudioEncoder **System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Enumerates the audio encoding formats. **Parameters**
| Name| Default Value| Description| | Name | Type | Mandatory| Description |
| ------ | ------ | ------------------------------------------------------------ | | -------- | -------- | ---- | ---------------------------------------- |
| AAC_LC | 3 | Advanced Audio Coding Low Complexity (AAC-LC).| | timeMs | number | Yes | Position to seek to, in milliseconds. |
| mode | SeekMode | Yes | Seek mode. For details, see [SeekMode](#seekmode8).|
| callback | function | Yes | Callback used to return the result. |
**Example**
## AudioOutputFormat ```js
videoPlayer.seek((seekTime, seekMode, err) => {
if (typeof (err) == 'undefined') {
console.info('seek success!');
} else {
console.info('seek fail!');
}
});
```
Enumerates the audio output formats. ### seek<sup>8+</sup>
| Name| Default Value| Description| seek(timeMs: number, mode?:SeekMode): Promise\<number>
| -------- | ------ | ------------------------------------------------------------ |
| MPEG_4 | 2 | MPEG-4.| Seeks to the specified playback position. If **mode** is not specified, the next key frame at the specified position is played. This API uses a promise to return the result.
| AAC_ADTS | 6 | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.|
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | -------- | ---- | -------------------------------------- |
| timeMs | number | Yes | Position to seek to, in milliseconds. |
| mode | SeekMode | No | Seek mode. For details, see [SeekMode](#seekmode8).|
**Return value**
| Type | Description |
| -------------- | ----------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.seek(seekTime).then((seekDoneTime) => { // seekDoneTime indicates the position after the seek operation is complete.
console.info('seek success');
}, failureCallback).catch(catchCallback);
await videoPlayer.seek(seekTime, seekMode).then((seekDoneTime) => {
console.info('seek success');
}, failureCallback).catch(catchCallback);
```
### setVolume<sup>8+</sup>
setVolume(vol: number, callback: AsyncCallback\<void>): void
Sets the volume. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).|
| callback | function | Yes | Callback used to return the result. |
**Example**
```js
videoPlayer.setVolume((vol, err) => {
if (typeof (err) == 'undefined') {
console.info('setVolume success!');
} else {
console.info('setVolume fail!');
}
});
```
### setVolume<sup>8+</sup>
setVolume(vol: number): Promise\<void>
Sets the volume. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).|
**Return value**
| Type | Description |
| -------------- | ------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.setVolume(vol).then() => {
console.info('setVolume success');
}, failureCallback).catch(catchCallback);
```
### release<sup>8+</sup>
release(callback: AsyncCallback\<void>): void
Releases the video playback resource. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes | Callback used to return the result.|
**Example**
```js
videoPlayer.release((err) => {
if (typeof (err) == 'undefined') {
console.info('release success!');
} else {
console.info('release fail!');
}
});
```
### release<sup>8+</sup>
release(): Promise\<void>
Releases the video playback resource. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value**
| Type | Description |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.release().then() => {
console.info('release success');
}, failureCallback).catch(catchCallback);
```
### getTrackDescription<sup>8+</sup>
getTrackDescription(callback: AsyncCallback<Array<[MediaDescription](#mediadescription8>>)>>): void
Obtains the video track information. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | -------------------------- |
| callback | function | Yes | Callback used to return the video track information obtained.|
**Example**
```js
function printfDescription(obj) {
for (let item in obj) {
let property = obj[item];
console.info('video key is ' + item);
console.info('video value is ' + property);
}
}
videoPlayer.getTrackDescription((error, arrlist) => {
if (typeof (arrlist) != 'undefined') {
for (let i = 0; i < arrlist.length; i++) {
printfDescription(arrlist[i]);
}
} else {
console.log(`video getTrackDescription fail, error:${error.message}`);
}
});
```
### getTrackDescription<sup>8+</sup>
getTrackDescription(): Promise<Array<[MediaDescription](#mediadescription8>>)>>
Obtains the video track information. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Return value**
| Type | Description |
| -------------------------------------------------------- | ------------------------------- |
| Promise<Array<[MediaDescription](#mediadescription8>>)>> | Promise used to return the video track information obtained.|
**Example**
```js
function printfDescription(obj) {
for (let item in obj) {
let property = obj[item];
console.info('video key is ' + item);
console.info('video value is ' + property);
}
}
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
let arrayDescription;
await videoPlayer.getTrackDescription().then((arrlist) => {
if (typeof (arrlist) != 'undefined') {
arrayDescription = arrlist;
} else {
console.log('video getTrackDescription fail');
}
}, failureCallback).catch(catchCallback);
for (let i = 0; i < arrayDescription.length; i++) {
printfDescription(arrayDescription[i]);
}
```
### setSpeed<sup>8+</sup>
setSpeed(speed:number, callback: AsyncCallback\<number>): void
Sets the video playback speed. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ---------------------------------------------------------- |
| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).|
| callback | function | Yes | Callback used to return the result. |
**Example**
```js
videoPlayer.setSpeed((speed:number, err) => {
if (typeof (err) == 'undefined') {
console.info('setSpeed success!');
} else {
console.info('setSpeed fail!');
}
});
```
### setSpeed<sup>8+</sup>
setSpeed(speed:number): Promise\<number>
Sets the video playback speed. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ---------------------------------------------------------- |
| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).|
**Example**
```js
function failureCallback(error) {
console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.setSpeed(speed).then() => {
console.info('setSpeed success');
}, failureCallback).catch(catchCallback);
```
### on('playbackCompleted')<sup>8+</sup>
on(type: 'playbackCompleted', callback: Callback\<void>): void
Subscribes to the video playback completion event.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ----------------------------------------------------------- |
| type | string | Yes | Type of the event to subscribe to, which is 'playbackCompleted' in this example.|
| callback | function | Yes | Callback invoked when the event is triggered. |
**Example**
```js
videoPlayer.on('playbackCompleted', () => {
console.info('playbackCompleted success!');
});
```
### on('bufferingUpdate')<sup>8+</sup>
on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void
Subscribes to the video buffering update event.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Type of the event to subscribe to, which is 'bufferingUpdate' in this example. |
| callback | function | Yes | Callback invoked when the event is triggered.<br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.|
**Example**
```js
videoPlayer.on('bufferingUpdate', (infoType, value) => {
console.log('video bufferingInfo type: ' + infoType);
console.log('video bufferingInfo value: ' + value);
});
```
### on('startRenderFrame')<sup>8+</sup>
on(type: 'startRenderFrame', callback: Callback\<void>): void
Subscribes to the frame rendering start event.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Type of the event to subscribe to, which is 'startRenderFrame' in this example.|
| callback | function | Yes | Callback invoked when the event is triggered. |
**Example**
```js
videoPlayer.on('startRenderFrame', () => {
console.info('startRenderFrame success!');
});
```
### on('videoSizeChanged')<sup>8+</sup>
on(type: 'videoSizeChanged', callback: (width: number, height: number) => void): void
Subscribes to the video width and height change event.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Type of the event to subscribe to, which is 'videoSizeChanged' in this example.|
| callback | function | Yes | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height. |
**Example**
```js
videoPlayer.on('videoSizeChanged', (width, height) => {
console.log('video width is: ' + width);
console.log('video height is: ' + height);
});
```
### on('error')<sup>8+</sup>
on(type: 'error', callback: ErrorCallback): void
Subscribes to the video playback error event.
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Type of the event to subscribe to, which is 'error' in this method.<br>The 'error' event is triggered when an error occurs during video playback.|
| callback | function | Yes | Callback invoked when the event is triggered. |
**Example**
```js
videoPlayer.on('error', (error) => { // Set the 'error' event callback.
console.info(`video error called, errName is ${error.name}`); // Print the error name.
console.info(`video error called, errCode is ${error.code}`); // Print the error code.
console.info(`video error called, errMessage is ${error.message}`);// Print the detailed description of the error type.
});
videoPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'error' event.
```
## VideoPlayState<sup>8+</sup>
Enumerates the video playback states. You can obtain the state through the **state** attribute.
| Name | Type | Description |
| -------- | ------ | ------------------------------------------------------------ |
| idle | string | The video player is idle.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| prepared | string | Video playback is being prepared.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| playing | string | Video playback is in progress.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| paused | string | Video playback is paused.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| stopped | string | Video playback is stopped.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| error | string | Video playback is in the error state.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
## SeekMode<sup>8+</sup>
Enumerates the video playback seek modes, which can be passed in the **seek** method.
| Name | Value | Description |
| -------------- | ---- | ------------------------------------------------------------ |
| SEEK_NEXT_SYNC | 0 | Seeks to the next key frame at the specified position. You are advised to use this value for the rewind operation.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| SEEK_PREV_SYNC | 1 | Seeks to the previous key frame at the specified position. You are advised to use this value for the fast-forward operation.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
## PlaybackSpeed<sup>8+</sup>
Enumerates the video playback speeds, which can be passed in the **setSpeed** method.
| Name | Value | Description |
| -------------------- | ---- | ------------------------------------------------------------ |
| SPEED_FORWARD_0_75_X | 0 | Plays the video at 0.75 times the normal speed.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| SPEED_FORWARD_1_00_X | 1 | Plays the video at the normal speed.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| SPEED_FORWARD_1_25_X | 2 | Plays the video at 1.25 times the normal speed.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| SPEED_FORWARD_1_75_X | 3 | Plays the video at 1.75 times the normal speed.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
| SPEED_FORWARD_2_00_X | 4 | Plays the video at 2.00 times the normal speed.<br>**System capability**: SystemCapability.Multimedia.Media.VideoPlayer|
## MediaDescription<sup>8+</sup>
### [key : string] : any
Defines media information in key-value mode.
| Name | Type | Description |
| ----- | ------ | ------------------------------------------------------------ |
| key | string | Key of the media information. For details about the keys, see [MediaDescriptionKey](#mediadescriptionkey8).<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| value | any | Value of the key. For details about the values, see [MediaDescriptionKey](#mediadescriptionkey8).<br>**System capability**: SystemCapability.Multimedia.Media.Core|
**Example**
```js
function printfItemDescription(obj, key) {
let property = obj[key];
console.info('audio key is ' + key);
console.info('audio value is ' + property);
}
audioPlayer.getTrackDescription((error, arrlist) => {
if (typeof (arrlist) != 'undefined') {
for (let i = 0; i < arrlist.length; i++) {
printfItemDescription(arrlist[i], MD_KEY_TRACK_TYPE); // Print the MD_KEY_TRACK_TYPE value of each track.
}
} else {
console.log(`audio getTrackDescription fail, error:${error.message}`);
}
});
```
## AudioRecorder
Implements audio recording. Before calling a method of the **AudioRecorder** class, you must call [createAudioRecorder()](#media.createaudiorecorder) to create an [AudioRecorder](#audiorecorder) instance.
For details about the audio recording demo, see [Audio Recording Development](../../media/audio-recorder.md).
### prepare<a name=audiorecorder_prepare></a>
prepare(config: AudioRecorderConfig): void
Prepares for recording.
**Required permissions:** ohos.permission.MICROPHONE
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ |
| config | [AudioRecorderConfig](#audiorecorderconfig) | Yes | Audio recording parameters, including the audio output URI, [encoding format](#audioencoder), sampling rate, number of audio channels, and [output format](#audiooutputformat).|
**Example**
```js
let audioRecorderConfig = {
audioEncoder : media.AudioEncoder.AAC_LC,
audioEncodeBitRate : 22050,
audioSampleRate : 22050,
numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS,
uri : 'fd://1', // The file must be created by the caller and granted with proper permissions.
location : { latitude : 30, longitude : 130},
}
audioRecorder.on('prepare', () => { // Set the 'prepare' event callback.
console.log('prepare success');
});
audioRecorder.prepare(audioRecorderConfig);
```
### start<a name=audiorecorder_start></a>
start(): void
Starts audio recording. This method can be called only after the [prepare](#audiorecorder_on) event is triggered.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Example**
```js
audioRecorder.on('start', () => { // Set the 'start' event callback.
console.log('audio recorder start success');
});
audioRecorder.start();
```
### pause<a name=audiorecorder_pause></a>
pause():void
Pauses audio recording. This method can be called only after the [start](#audiorecorder_on) event is triggered.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Example**
```js
audioRecorder.on('pause', () => { // Set the 'pause' event callback.
console.log('audio recorder pause success');
});
audioRecorder.pause();
```
### resume<a name=audiorecorder_resume></a>
resume():void
Resumes audio recording. This method can be called only after the [pause](#audiorecorder_on) event is triggered.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Example**
```js
audioRecorder.on('resume', () => { // Set the 'resume' event callback.
console.log('audio recorder resume success');
});
audioRecorder.resume();
```
### stop<a name=audiorecorder_stop></a>
stop(): void
Stops audio recording.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Example**
```js
audioRecorder.on('stop', () => { // Set the 'stop' event callback.
console.log('audio recorder stop success');
});
audioRecorder.stop();
```
### release<a name=audiorecorder_release></a>
release(): void
Releases the audio recording resource.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Example**
```js
audioRecorder.on('release', () => { // Set the 'release' event callback.
console.log('audio recorder release success');
});
audioRecorder.release();
audioRecorder = undefined;
```
### reset<a name=audiorecorder_reset></a>
reset(): void
Resets audio recording.
Before resetting audio recording, you must call [stop()](#audiorecorder_stop) to stop recording. After audio recording is reset, you must call [prepare()](#audiorecorder_prepare) to set the recording parameters for another recording.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Example**
```js
audioRecorder.on('reset', () => { // Set the 'reset' event callback.
console.log('audio recorder reset success');
});
audioRecorder.reset();
```
### on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<a name=audiorecorder_on></a>
on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void
Subscribes to the audio recording events.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Type of the event to subscribe to. The following events are supported: 'prepare'\|'start'\| 'pause' \| 'resume' \|'stop'\|'release'\|'reset'<br>- The 'prepare' event is triggered when the [prepare](#audiorecorder_prepare) method is called and the audio recording parameters are set.<br>- The 'start' event is triggered when the [start](#audiorecorder_start) method is called and audio recording starts.<br>- The 'pause' event is triggered when the [pause](#audiorecorder_pause) method is called and audio recording is paused.<br>- The 'resume' event is triggered when the [resume](#audiorecorder_resume) method is called and audio recording is resumed.<br>- The 'stop' event is triggered when the [stop](#audiorecorder_stop) method is called and audio recording stops.<br>- The 'release' event is triggered when the [release](#audiorecorder_release) method is called and the recording resource is released.<br>- The 'reset' event is triggered when the [reset](#audiorecorder_reset) method is called and audio recording is reset.|
| callback | ()=>void | Yes | Callback invoked when the event is triggered. |
**Example**
```js
let audiorecorder = media.createAudioRecorder(); // Create an AudioRecorder instance.
let audioRecorderConfig = {
audioEncoder : media.AudioEncoder.AAC_LC, ,
audioEncodeBitRate : 22050,
audioSampleRate : 22050,
numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS,
uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions.
location : { latitude : 30, longitude : 130},
}
audioRecorder.on('error', (error) => { // Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`);
console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`);
});
audioRecorder.on('prepare', () => { // Set the 'prepare' event callback.
console.log('prepare success');
audioRecorder.start(); // Start recording and trigger the 'start' event callback.
});
audioRecorder.on('start', () => { // Set the 'start' event callback.
console.log('audio recorder start success');
});
audioRecorder.on('pause', () => { // Set the 'pause' event callback.
console.log('audio recorder pause success');
});
audioRecorder.on('resume', () => { // Set the 'resume' event callback.
console.log('audio recorder resume success');
});
audioRecorder.on('stop', () => { // Set the 'stop' event callback.
console.log('audio recorder stop success');
});
audioRecorder.on('release', () => { // Set the 'release' event callback.
console.log('audio recorder release success');
});
audioRecorder.on('reset', () => { // Set the 'reset' event callback.
console.log('audio recorder reset success');
});
audioRecorder.prepare(audioRecorderConfig) // Set recording parameters and trigger the 'prepare' event callback.
```
### on('error')
on(type: 'error', callback: ErrorCallback): void
Subscribes to the audio recording error event.
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Type of the event to subscribe to, which is 'error' in this method.<br>The 'error' event is triggered when an error occurs during audio recording.|
| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. |
**Example**
```js
audioRecorder.on('error', (error) => { // Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`); // Print the error name.
console.info(`audio error called, errCode is ${error.code}`); // Print the error code.
console.info(`audio error called, errMessage is ${error.message}`); // Print the detailed description of the error type.
});
audioRecorder.prepare(); // Do no set any parameter in prepare and trigger the 'error' event callback.
```
## AudioRecorderConfig
Describes audio recording configurations.
| Name | Type | Mandatory| Description |
| --------------------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| audioEncoder | [AudioEncoder](#audioencoder) | No | Audio encoding format. The default value is **AAC_LC**.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| audioEncodeBitRate | number | No | Audio encoding bit rate. The default value is **48000**.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| audioSampleRate | number | No | Audio sampling rate. The default value is **48000**.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| numberOfChannels | number | No | Number of audio channels. The default value is **2**.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| format | [AudioOutputFormat](#audiooutputformat) | No | Audio output format. The default value is **MPEG_4**.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| location<sup>8+</sup> | [Location](#location8) | No | Geographical location of the recorded audio.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| uri | string | Yes | Audio output URI. Supported: fd://xx&nbsp;(fd&nbsp;number)<br>![en-us_image_0000001164217678](figures/en-us_image_url.png) <br>The file must be created by the caller and granted with proper permissions.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
## AudioEncoder
Enumerates the audio encoding formats.
| Name | Default Value| Description |
| ------- | ------ | ------------------------------------------------------------ |
| DEFAULT | 0 | Default audio encoding format, which is Adaptive Multi Rate-Narrow Band Speech Codec (AMR-NB).<br>This API is merely defined in OpenHarmony 3.1 Release and cannot be used currently. It can be used in OpenHarmony 3.1 MR.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| AMR_NB | 1 | AMR-NB.<br>This API is merely defined in OpenHarmony 3.1 Release and cannot be used currently. It can be used in OpenHarmony 3.1 MR.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| AMR_WB | 2 | Adaptive Multi Rate-Wide Band Speech Codec (AMR-WB).<br>This API is merely defined in OpenHarmony 3.1 Release and cannot be used currently. It can be used in OpenHarmony 3.1 MR.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| AAC_LC | 3 | Advanced Audio Coding Low Complexity (AAC-LC).<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| HE_AAC | 4 | High-Efficiency Advanced&nbsp;Audio&nbsp;Coding (HE_AAC).<br>This API is merely defined in OpenHarmony 3.1 Release and cannot be used currently. It can be used in OpenHarmony 3.1 MR.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
## AudioOutputFormat
Enumerates the audio output formats.
| Name | Default Value| Description |
| -------- | ------ | ------------------------------------------------------------ |
| DEFAULT | 0 | Default encapsulation format, which is MPEG-4.<br>This API is merely defined in OpenHarmony 3.1 Release and cannot be used currently. It can be used in OpenHarmony 3.1 MR.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| MPEG_4 | 2 | MPEG-4.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| AMR_NB | 3 | AMR_NB.<br>This API is merely defined in OpenHarmony 3.1 Release and cannot be used currently. It can be used in OpenHarmony 3.1 MR.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| AMR_WB | 4 | AMR_WB.<br>This API is merely defined in OpenHarmony 3.1 Release and cannot be used currently. It can be used in OpenHarmony 3.1 MR.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
| AAC_ADTS | 6 | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.<br>**System capability**: SystemCapability.Multimedia.Media.AudioRecorder|
## VideoRecorder<sup>8+</sup>
Implements video recording. Before calling a method of the **VideoRecorder** class, you must call [createVideoRecorder()](#media.createvideorecorder8) to create a [VideoRecorder](#videorecorder8) instance.
For details about the video recording demo, see [Video Recording Development](../../media/video-recorder.md).
### Attributes
| Name | Type | Readable| Writable| Description |
| ------------------ | ------------------------------------- | ---- | ---- | ---------------- |
| state<sup>8+</sup> | [VideoRecordState](#videorecordstate) | Yes | No | Video recording state.|
### prepare<sup>8+</sup><a name=videorecorder_prepare1></a>
prepare(config: VideoRecorderConfig, callback: AsyncCallback\<void>): void;
Sets video recording parameters in asynchronous mode. This API uses a callback to return the result.
**Required permissions:** ohos.permission.MICROPHONE ohos.permission.CAMERA
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------------------------------------- | ---- | ----------------------------------- |
| config | [VideoRecorderConfig](#videorecorderconfig) | Yes | Video recording parameters to set. |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result.|
**Example**
```js
let videoProfile = {
audioBitrate : 48000,
audioChannels : 2,
audioCodec : 'audio/mp4a-latm',
audioSampleRate : 48000,
fileFormat : 'mp4',
videoBitrate : 48000,
videoCodec : 'video/mp4v-es',
videoFrameWidth : 640,
videoFrameHeight : 480,
videoFrameRate : 30
}
let videoConfig = {
audioSourceType : 1,
videoSourceType : 0,
profile : videoProfile,
url : 'fd://xx', // The file must be created by the caller and granted with proper permissions.
orientationHint : 0,
location : { latitude : 30, longitude : 130 },
}
// asyncallback
let videoRecorder = null;
let events = require('events');
let eventEmitter = new events.EventEmitter();
eventEmitter.on('prepare', () => {
videoRecorder.prepare(videoConfig, (err) => {
if (typeof (err) == 'undefined') {
console.info('prepare success');
} else {
console.info('prepare failed and error is ' + err.message);
}
});
});
media.createVideoRecorder((err, recorder) => {
if (typeof (err) == 'undefined' && typeof (recorder) != 'undefined') {
videoRecorder = recorder;
console.info('createVideoRecorder success');
eventEmitter.emit('prepare'); // Trigger the 'prepare' event.
} else {
console.info('createVideoRecorder failed and error is ' + err.message);
}
});
```
### prepare<sup>8+</sup><a name=videorecorder_prepare2></a>
prepare(config: VideoRecorderConfig): Promise\<void>;
Sets video recording parameters in asynchronous mode. This API uses a promise to return the result.
**Required permissions:** ohos.permission.MICROPHONE ohos.permission.CAMERA
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------------------------------------------- | ---- | ------------------------ |
| config | [VideoRecorderConfig](#videorecorderconfig) | Yes | Video recording parameters to set.|
**Return value**
| Type | Description |
| -------------- | ---------------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
let videoProfile = {
audioBitrate : 48000,
audioChannels : 2,
audioCodec : 'audio/mp4a-latm',
audioSampleRate : 48000,
fileFormat : 'mp4',
videoBitrate : 48000,
videoCodec : 'video/mp4v-es',
videoFrameWidth : 640,
videoFrameHeight : 480,
videoFrameRate : 30
}
let videoConfig = {
audioSourceType : 1,
videoSourceType : 0,
profile : videoProfile,
url : 'fd://xx', // The file must be created by the caller and granted with proper permissions.
orientationHint : 0,
location : { latitude : 30, longitude : 130 },
}
// promise
let videoRecorder = null;
await media.createVideoRecorder().then((recorder) => {
if (typeof (recorder) != 'undefined') {
videoRecorder = recorder;
console.info('createVideoRecorder success');
} else {
console.info('createVideoRecorder failed');
}
}, (err) => {
console.info('error hanppend message is ' + err.message);
}).catch((err) => {
console.info('catch err error message is ' + err.message);
});
await videoRecorder.prepare(videoConfig).then(() => {
console.info('prepare success');
}, (err) => {
console.info('prepare failed and error is ' + err.message);
}).catch((err) => {
console.info('prepare failed and catch error is ' + err.message);
});
```
### getInputSurface<sup>8+</sup>
getInputSurface(callback: AsyncCallback\<string>): void;
Obtains the surface required for recording in asynchronous mode. This surface is provided for the caller. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding data.
Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp is based on the system startup time.
This method can be called only after [prepare()](#videorecorder_prepare1) is called.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------------------- | ---- | --------------------------- |
| callback | AsyncCallback\<string> | Yes | Callback used to obtain the result.|
**Example**
```js
// asyncallback
let surfaceID = null; // Surface ID passed to the external system.
videoRecorder.getInputSurface((err, surfaceId) => {
if (typeof (err) == 'undefined') {
console.info('getInputSurface success');
surfaceID = surfaceId;
} else {
console.info('getInputSurface failed and error is ' + err.message);
}
});
```
### getInputSurface<sup>8+</sup>
getInputSurface(): Promise\<string>;
Obtains the surface required for recording in asynchronous mode. This surface is provided for the caller. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding data.
Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp is based on the system startup time.
This method can be called only after [prepare()](#videorecorder_prepare1) is called.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| ---------------- | -------------------------------- |
| Promise\<string> | Promise used to return the result.|
**Example**
```js
// promise
let surfaceID = null; // Surface ID passed to the external system.
await videoRecorder.getInputSurface().then((surfaceId) => {
console.info('getInputSurface success');
surfaceID = surfaceId;
}, (err) => {
console.info('getInputSurface failed and error is ' + err.message);
}).catch((err) => {
console.info('getInputSurface failed and catch error is ' + err.message);
});
```
### start<sup>8+</sup><a name=videorecorder_start1></a>
start(callback: AsyncCallback\<void>): void;
Starts video recording in asynchronous mode. This API uses a callback to return the result.
This method can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) are called, because the data source must pass data to the surface first.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result.|
**Example**
```js
// asyncallback
videoRecorder.start((err) => {
if (typeof (err) == 'undefined') {
console.info('start videorecorder success');
} else {
console.info('start videorecorder failed and error is ' + err.message);
}
});
```
### start<sup>8+</sup><a name=videorecorder_start2></a>
start(): Promise\<void>;
Starts video recording in asynchronous mode. This API uses a promise to return the result.
This method can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) are called, because the data source must pass data to the surface first.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
// promise
await videoRecorder.start().then(() => {
console.info('start videorecorder success');
}, (err) => {
console.info('start videorecorder failed and error is ' + err.message);
}).catch((err) => {
console.info('start videorecorder failed and catch error is ' + err.message);
});
```
### pause<sup>8+</sup><a name=videorecorder_pause1></a>
pause(callback: AsyncCallback\<void>): void;
Pauses video recording in asynchronous mode. This API uses a callback to return the result.
This method can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1).
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result.|
**Example**
```js
// asyncallback
videoRecorder.pause((err) => {
if (typeof (err) == 'undefined') {
console.info('pause videorecorder success');
} else {
console.info('pause videorecorder failed and error is ' + err.message);
}
});
```
### pause<sup>8+</sup><a name=videorecorder_pause2></a>
pause(): Promise\<void>;
Pauses video recording in asynchronous mode. This API uses a promise to return the result.
This method can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1).
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
// promise
await videoRecorder.pause().then(() => {
console.info('pause videorecorder success');
}, (err) => {
console.info('pause videorecorder failed and error is ' + err.message);
}).catch((err) => {
console.info('pause videorecorder failed and catch error is ' + err.message);
});
```
### resume<sup>8+</sup><a name=videorecorder_resume1></a>
resume(callback: AsyncCallback\<void>): void;
Resumes video recording in asynchronous mode. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result.|
**Example**
```js
// asyncallback
videoRecorder.resume((err) => {
if (typeof (err) == 'undefined') {
console.info('resume videorecorder success');
} else {
console.info('resume videorecorder failed and error is ' + err.message);
}
});
```
### resume<sup>8+</sup><a name=videorecorder_resume2></a>
resume(): Promise\<void>;
Resumes video recording in asynchronous mode. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
// promise
await videoRecorder.resume().then(() => {
console.info('resume videorecorder success');
}, (err) => {
console.info('resume videorecorder failed and error is ' + err.message);
}).catch((err) => {
console.info('resume videorecorder failed and catch error is ' + err.message);
});
```
### stop<sup>8+</sup><a name=videorecorder_stop1></a>
stop(callback: AsyncCallback\<void>): void;
Stops video recording in asynchronous mode. This API uses a callback to return the result.
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result.|
**Example**
```js
// asyncallback
videoRecorder.stop((err) => {
if (typeof (err) == 'undefined') {
console.info('stop videorecorder success');
} else {
console.info('stop videorecorder failed and error is ' + err.message);
}
});
```
### stop<sup>8+</sup><a name=videorecorder_stop2></a>
stop(): Promise\<void>;
Stops video recording in asynchronous mode. This API uses a promise to return the result.
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
// promise
await videoRecorder.stop().then(() => {
console.info('stop videorecorder success');
}, (err) => {
console.info('stop videorecorder failed and error is ' + err.message);
}).catch((err) => {
console.info('stop videorecorder failed and catch error is ' + err.message);
});
```
### release<sup>8+</sup><a name=videorecorder_release1></a>
release(callback: AsyncCallback\<void>): void;
Releases the video recording resource in asynchronous mode. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | -------------------------------- |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result.|
**Example**
```js
// asyncallback
videoRecorder.release((err) => {
if (typeof (err) == 'undefined') {
console.info('release videorecorder success');
} else {
console.info('release videorecorder failed and error is ' + err.message);
}
});
```
### release<sup>8+</sup><a name=videorecorder_release2></a>
release(): Promise\<void>;
Releases the video recording resource in asynchronous mode. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| -------------- | ----------------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
// promise
await videoRecorder.release().then(() => {
console.info('release videorecorder success');
}, (err) => {
console.info('release videorecorder failed and error is ' + err.message);
}).catch((err) => {
console.info('release videorecorder failed and catch error is ' + err.message);
});
```
### reset<sup>8+</sup><a name=videorecorder_reset1></a>
reset(callback: AsyncCallback\<void>): void;
Resets video recording in asynchronous mode. This API uses a callback to return the result.
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result.|
**Example**
```js
// asyncallback
videoRecorder.reset((err) => {
if (typeof (err) == 'undefined') {
console.info('reset videorecorder success');
} else {
console.info('reset videorecorder failed and error is ' + err.message);
}
});
```
### reset<sup>8+</sup><a name=videorecorder_reset2></a>
reset(): Promise\<void>;
Resets video recording in asynchronous mode. This API uses a promise to return the result.
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Return value**
| Type | Description |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
// promise
await videoRecorder.reset().then(() => {
console.info('reset videorecorder success');
}, (err) => {
console.info('reset videorecorder failed and error is ' + err.message);
}).catch((err) => {
console.info('reset videorecorder failed and catch error is ' + err.message);
});
```
### on('error')<sup>8+</sup>
on(type: 'error', callback: ErrorCallback): void
Subscribes to the video recording error event.
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Type of the event to subscribe to, which is 'error' in this method.<br>The 'error' event is triggered when an error occurs during video recording.|
| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. |
**Example**
```js
videoRecorder.on('error', (error) => { // Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`); // Print the error name.
console.info(`audio error called, errCode is ${error.code}`); // Print the error code.
console.info(`audio error called, errMessage is ${error.message}`); // Print the detailed description of the error type.
});
// This event is reported when an error occurs during the retrieval of videoRecordState.
```
## VideoRecordState<sup>8+</sup>
Enumerates the video recording states. You can obtain the state through the **state** attribute.
| Name | Type | Description |
| -------- | ------ | ------------------------------------------------------------ |
| idle | string | The video recorder is idle.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| prepared | string | The video recording parameters are set.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| playing | string | Video recording is in progress.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| paused | string | Video recording is paused.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| stopped | string | Video recording is stopped.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| error | string | Video recording is in the error state.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
## VideoRecorderConfig<sup>8+</sup>
Describes the video recording parameters.
| Name | Type | Mandatory| Description |
| --------------- | ---------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| audioSourceType | [AudioSourceType](#audiosourcetype<sup>8+</sup>) | Yes | Type of the audio source for video recording.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| videoSourceType | [VideoSourceType](#videosourcetype<sup>8+</sup>) | Yes | Type of the video source for video recording.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| profile | [VideoRecorderProfile](#videorecorderprofile<sup>8+</sup>) | Yes | Video recording profile.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| orientationHint | number | No | Rotation angle of the recorded video.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| location | [Location](#location8) | No | Geographical location of the recorded video.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| url | string | Yes | Video output URL. Supported: fd://xx&nbsp;(fd&nbsp;number)<br>![en-us_image_0000001164217678](figures/en-us_image_url.png) <br>The file must be created by the caller and granted with proper permissions.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
## AudioSourceType<sup>8+</sup>
Enumerates the audio source types for video recording.
| Name | Value | Description |
| ------------------------- | ---- | ------------------------------------------------------------ |
| AUDIO_SOURCE_TYPE_DEFAULT | 0 | Default audio input source.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| AUDIO_SOURCE_TYPE_MIC | 1 | Mic audio input source.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
## VideoSourceType<sup>8+</sup>
Enumerates the video source types for video recording.
| Name | Value | Description |
| ----------------------------- | ---- | ------------------------------------------------------------ |
| VIDEO_SOURCE_TYPE_SURFACE_YUV | 0 | The input surface carries raw data.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| VIDEO_SOURCE_TYPE_SURFACE_ES | 1 | The input surface carries ES data.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
## VideoRecorderProfile<sup>8+</sup>
Describes the video recording profile.
| Name | Type | Mandatory| Description |
| ---------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| audioBitrate | number | Yes | Audio encoding bit rate.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| audioChannels | number | Yes | Number of audio channels.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| audioCodec | [CodecMimeType](#CodecMimeType8) | Yes | Audio encoding format.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| audioSampleRate | number | Yes | Audio sampling rate.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| fileFormat | [ContainerFormatType](#containerformattype8) | Yes | Container format of a file.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| videoCodec | [CodecMimeType](#CodecMimeType8) | Yes | Video encoding format.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| videoFrameWidth | number | Yes | Width of the recorded video frame.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
| videoFrameHeight | number | Yes | Height of the recorded video frame.<br>**System capability**: SystemCapability.Multimedia.Media.VideoRecorder|
## ContainerFormatType<sup>8+</sup>
Enumerates the container format types (CFTs).
| Name | Value | Description |
| ----------- | ----- | ------------------------------------------------------------ |
| CFT_MPEG_4 | "mp4" | Video container format MP4.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| CFT_MPEG_4A | "m4a" | Audio container format M4A.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
## Location<sup>8+</sup>
Describes the geographical location of the recorded video.
| Name | Type| Mandatory| Description |
| --------- | -------- | ---- | ------------------------------------------------------------ |
| latitude | number | Yes | Latitude of the geographical location.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
| longitude | number | Yes | Longitude of the geographical location.<br>**System capability**: SystemCapability.Multimedia.Media.Core|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册