提交 93e1a285 编写于 作者: W wusongqing

updated docs

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 7f331b1e
此差异已折叠。
...@@ -16,21 +16,20 @@ During video recording, audio and video signals are captured, encoded, and saved ...@@ -16,21 +16,20 @@ During video recording, audio and video signals are captured, encoded, and saved
## How to Develop ## How to Develop
For details about the APIs used for video recording, see [VideoRecorder in the Media API](../reference/apis/js-apis-media.md). For details about the APIs, see [VideoRecorder in the Media API](../reference/apis/js-apis-media.md).
### Full-Process Scenario ### Full-Process Scenario
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, starting, pausing, resuming, and stopping recording, and releasing resources.
```js ```js
import media from '@ohos.multimedia.media' import media from '@ohos.multimedia.media'
import mediaLibrary from '@ohos.multimedia.mediaLibrary' import mediaLibrary from '@ohos.multimedia.mediaLibrary'
export class VideoRecorderDemo {
let testFdNumber; private testFdNumber; // Used to save the FD address.
// pathName indicates the passed recording file name, for example, 01.mp4. The generated file address is /storage/media/100/local/files/Video/01.mp4.
// 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.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA. async getFd(pathName) {
async function getFd(pathName) {
let displayName = pathName; let displayName = pathName;
const mediaTest = mediaLibrary.getMediaLibrary(); const mediaTest = mediaLibrary.getMediaLibrary();
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
...@@ -38,110 +37,115 @@ async function getFd(pathName) { ...@@ -38,110 +37,115 @@ async function getFd(pathName) {
let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO); let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath); let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
if (dataUri != undefined) { if (dataUri != undefined) {
let args = dataUri.id.toString(); let args = dataUri.id.toString();
let fetchOp = { let fetchOp = {
selections : fileKeyObj.ID + "=?", selections : fileKeyObj.ID + "=?",
selectionArgs : [args], selectionArgs : [args],
} }
let fetchFileResult = await mediaTest.getFileAssets(fetchOp); let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
let fileAsset = await fetchFileResult.getAllObject(); let fileAsset = await fetchFileResult.getAllObject();
let fdNumber = await fileAsset[0].open('Rw'); let fdNumber = await fileAsset[0].open('Rw');
fdNumber = "fd://" + fdNumber.toString(); this.testFdNumber = "fd://" + fdNumber.toString();
testFdNumber = fdNumber; }
}
// Error callback triggered in the case of an error
failureCallback(error) {
console.info('error happened, error name is ' + error.name);
console.info('error happened, error code is ' + error.code);
console.info('error happened, error message is ' + error.message);
}
// Error callback triggered in the case of an exception
catchCallback(error) {
console.info('catch error happened, error name is ' + error.name);
console.info('catch error happened, error code is ' + error.code);
console.info('catch error happened, error message is ' + error.message);
}
async videoRecorderDemo() {
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.
// Obtain the FD address of the video to be recorded.
await this.getFd('01.mp4');
// Recording-related parameter settings
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
} }
}
await getFd('01.mp4');
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 = { let videoConfig = {
audioSourceType : 1, audioSourceType : 1,
videoSourceType : 0, videoSourceType : 0,
profile : videoProfile, profile : videoProfile,
url: testFdNumber, // testFdNumber is generated by getFd. url : this.testFdNumber, // testFdNumber is generated by getFd.
orientationHint : 0, orientationHint : 0,
location : { latitude : 30, longitude : 130 }, location : { latitude : 30, longitude : 130 },
} }
// Create a VideoRecorder object.
// Error callback triggered in the case of an error await media.createVideoRecorder().then((recorder) => {
function failureCallback(error) { console.info('case createVideoRecorder called');
console.info('error happened, error name is ' + error.name); if (typeof (recorder) != 'undefined') {
console.info('error happened, error code is ' + error.code);
console.info('error happened, error message is ' + error.message);
}
// Error callback triggered in the case of an exception
function catchCallback(error) {
console.info('catch error happened, error name is ' + error.name);
console.info('catch error happened, error code is ' + error.code);
console.info('catch error happened, error message is ' + error.message);
}
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.
// Create a VideoRecorder object.
await media.createVideoRecorder().then((recorder) => {
console.info('case createVideoRecorder called');
if (typeof (recorder) != 'undefined') {
videoRecorder = recorder; videoRecorder = recorder;
console.info('createVideoRecorder success'); console.info('createVideoRecorder success');
} else { } else {
console.info('createVideoRecorder failed'); console.info('createVideoRecorder failed');
} }
}, failureCallback).catch(catchCallback); }, this.failureCallback).catch(this.catchCallback);
// Obtain the surface ID, save it, and pass it to camera-related interfaces. // Call the prepare API to prepare for video recording.
await videoRecorder.getInputSurface().then((surface) => { await videoRecorder.prepare(videoConfig).then(() => {
console.info('getInputSurface success'); console.info('prepare success');
surfaceID = surface; }, this.failureCallback).catch(this.catchCallback);
}, failureCallback).catch(catchCallback);
// Obtain the surface ID, save it, and pass it to camera-related APIs.
// Video recording depends on camera-related interfaces. The following operations can be performed only after the video output start interface is invoked. await videoRecorder.getInputSurface().then((surface) => {
console.info('getInputSurface success');
// Start video recording. surfaceID = surface;
await videoRecorder.start().then(() => { }, this.failureCallback).catch(this.catchCallback);
console.info('start success');
}, failureCallback).catch(catchCallback); // Video recording depends on camera-related APIs. The following operations can be performed only after the video output start API is invoked. For details about how to call the camera APIs, see the samples.
// Start video recording.
// Pause video playback before the video output stop interface is invoked. await videoRecorder.start().then(() => {
await videoRecorder.pause().then(() => { console.info('start success');
console.info('pause success'); }, this.failureCallback).catch(this.catchCallback);
}, failureCallback).catch(catchCallback);
// Pause video recording before the video output stop API of the camera is invoked.
// Resume video playback after the video output start interface is invoked. await videoRecorder.pause().then(() => {
await videoRecorder.resume().then(() => { console.info('pause success');
console.info('resume success'); }, this.failureCallback).catch(this.catchCallback);
}, failureCallback).catch(catchCallback);
// Resume video recording after the video output start API of the camera is invoked.
// Stop video recording after the video output stop interface is invoked. await videoRecorder.resume().then(() => {
await videoRecorder.stop().then(() => { console.info('resume success');
console.info('stop success'); }, this.failureCallback).catch(this.catchCallback);
}, failureCallback).catch(catchCallback);
// Stop video recording after the video output stop API of the camera is invoked.
// Reset the recording configuration. await videoRecorder.stop().then(() => {
await videoRecorder.reset().then(() => { console.info('stop success');
console.info('reset success'); }, this.failureCallback).catch(this.catchCallback);
}, failureCallback).catch(catchCallback);
// Reset the recording configuration.
// Release the video recording resources and camera object resources. await videoRecorder.reset().then(() => {
await videoRecorder.release().then(() => { console.info('reset success');
console.info('release success'); }, this.failureCallback).catch(this.catchCallback);
}, failureCallback).catch(catchCallback);
// Release the video recording resources and camera object resources.
// Set the related object to null. await videoRecorder.release().then(() => {
videoRecorder = null; console.info('release success');
surfaceID = null; }, this.failureCallback).catch(this.catchCallback);
// Set the related object to null.
videoRecorder = undefined;
surfaceID = undefined;
}
}
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册