1# Moving Photos (ArkTS) 2 3The 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. 4 5To develop the moving photo feature, perform the following steps: 6 7- Check whether the device supports taking moving photos. 8- Enable the capability of taking moving photos (if supported). 9- 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). 10 11## How to Develop 12 13Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 14 15> **NOTE** 16> 17> - Before enabling the capability of taking moving photos, you must enable [deferred photo delivery](camera-deferred-capture.md). 18> - The permission **ohos.permission.MICROPHONE** is required for taking moving photos. For details about how to apply for and verify the permission, see [Camera Development Preparations](camera-preparation.md). Otherwise, there is no sound when a photo is being taken. 19 201. Import dependencies. Specifically, import the camera, image, and mediaLibrary modules. 21 22 ```ts 23 import { camera } from '@kit.CameraKit'; 24 import { photoAccessHelper } from '@kit.MediaLibraryKit'; 25 import { BusinessError } from '@kit.BasicServicesKit'; 26 ``` 27 282. Determine the photo output stream. 29 30 You can use the **photoProfiles** attribute of the [CameraOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability) class to obtain the photo output streams supported by the device and use [createPhotoOutput](../../reference/apis-camera-kit/js-apis-camera.md#createphotooutput11) to create a photo output stream. 31 32 ```ts 33 function getPhotoOutput(cameraManager: camera.CameraManager, 34 cameraOutputCapability: camera.CameraOutputCapability): camera.PhotoOutput | undefined { 35 let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles; 36 if (!photoProfilesArray) { 37 console.error("createOutput photoProfilesArray == null || undefined"); 38 } 39 let photoOutput: camera.PhotoOutput | undefined = undefined; 40 try { 41 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]); 42 } catch (error) { 43 let err = error as BusinessError; 44 console.error(`Failed to createPhotoOutput. error: ${JSON.stringify(err)}`); 45 } 46 return photoOutput; 47 } 48 ``` 49 503. Check whether the device supports taking moving photos. 51 52 ```ts 53 function isMovingPhotoSupported(photoOutput: camera.PhotoOutput): boolean { 54 let isSupported: boolean = false; 55 try { 56 isSupported = photoOutput.isMovingPhotoSupported(); 57 } catch (error) { 58 // If the operation fails, error.code is returned and processed. 59 let err = error as BusinessError; 60 console.error(`The isMovingPhotoSupported call failed. error code: ${err.code}`); 61 } 62 return isSupported; 63 } 64 ``` 65 664. Enable the capability of taking moving photos. 67 68 ```ts 69 function enableMovingPhoto(photoOutput: camera.PhotoOutput): void { 70 try { 71 photoOutput.enableMovingPhoto(true); 72 } catch (error) { 73 // If the operation fails, error.code is returned and processed. 74 let err = error as BusinessError; 75 console.error(`The enableMovingPhoto call failed. error code: ${err.code}`); 76 } 77 } 78 ``` 79 805. Trigger photographing. This procedure is the same as that in the common photographing mode. For details, see [Camera Photographing](camera-shooting.md). 81 82 83 84## Status Listening 85 86Register a callback function to listen for **'photoAssetAvailable'** events. 87 88 ```ts 89 let context = getContext(this); 90 let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 91 92 async function mediaLibSavePhoto(photoAsset: photoAccessHelper.PhotoAsset): Promise<void> { 93 try { 94 let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset); 95 assetChangeRequest.saveCameraPhoto(); 96 await phAccessHelper.applyChanges(assetChangeRequest); 97 console.info('apply saveCameraPhoto successfully'); 98 } catch (err) { 99 console.error(`apply saveCameraPhoto failed with error: ${err.code}, ${err.message}`); 100 } 101 } 102 103 function onPhotoOutputPhotoAssetAvailable(photoOutput: camera.PhotoOutput): void { 104 photoOutput.on('photoAssetAvailable', (err: BusinessError, photoAsset: photoAccessHelper.PhotoAsset): void => { 105 if (err) { 106 console.info(`photoAssetAvailable error: ${JSON.stringify(err)}.`); 107 return; 108 } 109 console.info('photoOutPutCallBack photoAssetAvailable'); 110 // Call the mediaLibrary flush API to save the first-phase images and moving photos. 111 mediaLibSavePhoto(photoAsset); 112 }); 113 } 114 ``` 115 116 <!--no_check-->