• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Moving Photos (ArkTS)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8The camera framework provides the capability of taking moving photos. With this capability, users can take a moving photo in one-click mode, in a way similar to taking an ordinary photo.
9
10To develop the moving photo feature, perform the following steps:
11
12- Check whether the device supports taking moving photos.
13- Enable the capability of taking moving photos (if supported).
14- Listen for the photo callback function and save the photo to the media library. For details, see [Accessing and Managing Moving Photos](../medialibrary/photoAccessHelper-movingphoto.md).
15
16## How to Develop
17
18Read [Module Description](../../reference/apis-camera-kit/arkts-apis-camera.md) for the API reference.
19
20> **NOTE**
21>
22> - Before enabling the capability of taking moving photos, you must enable [deferred photo delivery](camera-deferred-capture.md).
23> - The permission ohos.permission.MICROPHONE is required for taking moving photos. For details about how to apply for and verify the permission, see [Requesting Camera Development Permissions](camera-preparation.md). Otherwise, there is no sound when a photo is being taken.
24
251. Import dependencies. Specifically, import the camera, image, and mediaLibrary modules.
26
27   ```ts
28   import { camera } from '@kit.CameraKit';
29   import { photoAccessHelper } from '@kit.MediaLibraryKit';
30   import { BusinessError } from '@kit.BasicServicesKit';
31   ```
32
332. Determine the photo output stream.
34
35   You can use the **photoProfiles** property of the [CameraOutputCapability](../../reference/apis-camera-kit/arkts-apis-camera-i.md#cameraoutputcapability) class to obtain the photo output streams supported by the device and use [createPhotoOutput](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#createphotooutput11) to create a photo output stream.
36
37   ```ts
38   function getPhotoOutput(cameraManager: camera.CameraManager,
39                           cameraOutputCapability: camera.CameraOutputCapability): camera.PhotoOutput | undefined {
40     let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles;
41     if (!photoProfilesArray) {
42       console.error("createOutput photoProfilesArray == null || undefined");
43     }
44     let photoOutput: camera.PhotoOutput | undefined = undefined;
45     try {
46       photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]);
47     } catch (error) {
48       let err = error as BusinessError;
49       console.error(`Failed to createPhotoOutput. error: ${err}`);
50     }
51     return photoOutput;
52   }
53   ```
54
553. Check whether the device supports taking moving photos.
56
57    > **NOTE**
58    >
59    > Before the check, you must configure, commit, and start a session. For details, see [Camera Session Management](camera-session-management.md).
60
61    ```ts
62    function isMovingPhotoSupported(photoOutput: camera.PhotoOutput): boolean {
63      let isSupported: boolean = false;
64      try {
65        isSupported = photoOutput.isMovingPhotoSupported();
66      } catch (error) {
67        // If the operation fails, error.code is returned and processed.
68        let err = error as BusinessError;
69        console.error(`The isMovingPhotoSupported call failed. error code: ${err.code}`);
70      }
71      return isSupported;
72    }
73    ```
74
754. Enable the capability of taking moving photos.
76
77   ```ts
78   function enableMovingPhoto(photoOutput: camera.PhotoOutput): void {
79     try {
80       photoOutput.enableMovingPhoto(true);
81     } catch (error) {
82       // If the operation fails, error.code is returned and processed.
83       let err = error as BusinessError;
84       console.error(`The enableMovingPhoto call failed. error code: ${err.code}`);
85     }
86   }
87   ```
88
895. Trigger photo capture. This procedure is the same as that in the common photo capture mode. For details, see [Photo Capture](camera-shooting.md).
90
91## Status Listening
92
93During camera application development, you can listen for the output stream status of moving photos by registering the **'photoAsset'** event. This event can be registered when a PhotoOutput instance is created.
94
95   ```ts
96   function getPhotoAccessHelper(context: Context): photoAccessHelper.PhotoAccessHelper {
97     let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
98     return phAccessHelper;
99   }
100
101   async function mediaLibSavePhoto(photoAsset: photoAccessHelper.PhotoAsset,
102     phAccessHelper: photoAccessHelper.PhotoAccessHelper): Promise<void> {
103     try {
104       let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset);
105       assetChangeRequest.saveCameraPhoto();
106       await phAccessHelper.applyChanges(assetChangeRequest);
107       console.info('apply saveCameraPhoto successfully');
108     } catch (err) {
109       console.error(`apply saveCameraPhoto failed with error: ${err.code}, ${err.message}`);
110     }
111   }
112
113   function onPhotoOutputPhotoAssetAvailable(photoOutput: camera.PhotoOutput, context: Context): void {
114     photoOutput.on('photoAssetAvailable', (err: BusinessError, photoAsset: photoAccessHelper.PhotoAsset): void => {
115       if (err) {
116         console.error(`photoAssetAvailable error: ${err}.`);
117         return;
118       }
119       console.info('photoOutPutCallBack photoAssetAvailable');
120       // Call the mediaLibrary flush API to save the first-phase images and moving photos.
121       mediaLibSavePhoto(photoAsset, getPhotoAccessHelper(context));
122     });
123   }
124   ```
125