• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Multimedia Development
2
3
4## How do I obtain the frame data of a camera when using the XComponent to display the preview output stream of the camera? (API version 9)
5
6**Symptom**
7
8Currently, the API does not support real-time preview of the camera frame data. To obtain the frame data, you must bind an action, for example, photographing.
9
10**Solution**
11
12Create a dual-channel preview to obtain the frame data.
13
141. Use the XComponent to create a preview stream.
15
16   ```
17   // Obtain a PreviewOutput instance.
18   const surfaceId = globalThis.mxXComponentController.getXComponentSurfaceld();
19   this.mPreviewOutput = await Camera.createPreviewOutput(surfaceld) ;
20   ```
21
222. Use imageReceiver to listen for image information.
23
24   ```
25   // Add dual-channel preview.
26   const fullWidth = this.mFullScreenSize.width;
27   const fullHeight = this.mFullScreenSize.height;
28   const imageReceiver = await image.createImageReceiver(fullwidth, fullHeight,
29     formatImage, capacityImage) ;
30   const photoSurfaceId = await imageReceiver.getReceivingSurfaceld();
31   this.mPreviewOutputDouble = await Camera.createPreviewOutput ( photoSurfaceld)
32   ```
33
34
35## How do I obtain the preview image of the front camera? (API version 9)
36
37**Solution**
38
391. Use the **\@ohos.multimedia.camera** module to obtain the physical camera information.
40
41   ```
42   let cameraManager = await camera.getCameraManager(context);
43   let camerasInfo = await cameraManager.getSupportedCameras();
44   let cameraDevice = this.camerasInfo[0];
45   ```
46
472. Create and start the input stream channel of the physical camera.
48
49   ```
50   let cameraInput = await cameraManager.createCameraInput(cameraDevice);
51   await this.cameraInput.open();
52   ```
53
543. Obtain the output formats supported by the camera, and create a preview output channel based on the surface ID provided by the XComponent.
55
56   ```
57   let outputCapability = await this.cameraManager.getSupportedOutputCapability(cameraDevice);
58   let previewProfile = this.outputCapability.previewProfiles[0];
59   let previewOutput = await cameraManager.createPreviewOutput(previewProfile, previewId);
60   ```
61
624. Create a camera session, add the camera input stream and preview output stream to the session, and start the session. The preview image is displayed on the XComponent.
63
64   ```
65   let captureSession = await cameraManager.createCaptureSession();
66   await captureSession.beginConfig();
67   await captureSession.addInput(cameraInput);
68   await captureSession.addOutput(previewOutput);
69   await this.captureSession.commitConfig()
70   await this.captureSession.start();
71   ```
72
73
74## How do I set the camera focal length? (API version 9)
75
76**Solution**
77
781. Check whether the camera is a front camera. A front camera does not support focal length setting.
79
802. Use **captureSession.getZoomRatioRange()** to obtain the focal length range supported by the device.
81
823. Check whether the target focal length is within the range obtained. If yes, call **captureSession.setZoomRatio()** to set the focal length.
83
84
85## What should I do when multiple video components cannot be used for playback? (API version 9)
86
87**Symptom**
88
89A large number of video components are created. They cannot play media normally or even crash.
90
91**Solution**
92
93A maximum of 13 media player instances can be created.
94
95
96## How do I invoke the image library directly? (API version 9)
97
98**Solution**
99
100```
101let want = {
102  bundleName: 'com.ohos.photos',
103  abilityName: 'com.ohos.photos.MainAbility',
104  parameters: {
105 uri: 'detail'
106  }
107};
108let context = getContext(this) as common.UIAbilityContext;
109context.startAbility(want);
110```
111
112
113## How do I apply for the media read/write permission on a device? (API version 9)
114
115Applicable to: stage model
116
117**Solution**
118
1191. Configure the permissions **ohos.permission.READ_MEDIA** and **ohos.permission.WRITE_MEDIA** in the **module.json5** file.
120
121   Example:
122
123   ```
124   {
125     "module" : {
126       "requestPermissions":[
127         {
128           "name" : "ohos.permission.READ_MEDIA",
129           "reason": "$string:reason"
130         },
131         {
132           "name" : "ohos.permission.WRITE_MEDIA",
133           "reason": "$string:reason"
134         }
135       ]
136     }
137   }
138   ```
139
1402. Call **requestPermissionsFromUser** to request the permissions from end users in the form of a dialog box. This operation is required because the grant mode of both permissions is **user_grant**.
141
142   ```
143   let context = getContext(this) as common.UIAbilityContext;
144   let atManager = abilityAccessCtrl.createAtManager();
145   let permissions: Array<string> = ['ohos.permission.READ_MEDIA','ohos.permission.WRITE_MEDIA']
146   atManager.requestPermissionsFromUser(context, permissions)
147   .then((data) => {
148       console.log("Succeed to request permission from user with data: " + JSON.stringify(data))
149   }).catch((error) => {
150       console.log("Failed to request permission from user with error: " + JSON.stringify(error))
151   })
152   ```
153
154
155## How do I obtain the camera status? (API version 9)
156
157Applicable to: stage model
158
159**Solution**
160
161The **cameraManager** class provides a listener to subscribe to the camera status.
162
163```
164cameraManager.on('cameraStatus', (cameraStatusInfo) => {
165  console.log(`camera : ${cameraStatusInfo.camera.cameraId}`);
166  console.log(`status: ${cameraStatusInfo.status}`);
167})
168```
169
170CameraStatus
171
172Enumerates the camera statuses.
173
174**CAMERA_STATUS_APPEAR** (0): A camera appears.
175
176**CAMERA_STATUS_DISAPPEAR** (1): The camera disappears.
177
178**CAMERA_STATUS_AVAILABLE** (2): The camera is available.
179
180**CAMERA_STATUS_UNAVAILABLE** (3): The camera is unavailable.
181
182**References**
183
184[CameraStatus](../reference/apis-camera-kit/js-apis-camera.md#oncamerastatus)
185
186## Does SoundPool support audio in WMV format? Which formats are supported? (API version 10)
187
188**Solution**
189
190Currently, WMV is not supported. The supported formats are AAC, MPEG (MP3), FLAC, and Vorbis.
191
192**References**
193
194The formats supported by **SoundPool** are the same as those supported by the bottom layer. For details about the supported formats, see [Audio Decoding](../media/audio-decoding.md).
195
196## How do I read the preview image of the camera? (API version 10)
197
198**Solution**
199
200You can call **ImageReceiver.readLatestImage** to obtain the preview image of the camera.
201
202**References**
203
204[readLatestImage](../reference/apis-image-kit/js-apis-image.md#readlatestimage9)
205
206## How do I listen for recordings? (API version 10)
207
208**Solution**
209
210Audio-related listening of the system is implemented in **AudioStreamManager**. You can call **on(type: 'audioCapturerChange', callback: Callback<AudioCapturerChangeInfoArray>): void** to listen for audio capturer changes.
211
212**References**
213
214[onaudiocapturerchange](../reference/apis-audio-kit/js-apis-audio.md#onaudiocapturerchange9)
215
216## In which audio processing scenarios are 3A algorithms (AEC, ANC, and AGC) embedded? If they are embedded, is there any API related to audio 3A processing? How do I call them? Are independent switches provided for the 3A algorithms? Does the system support 3A in recording scenarios? If not, what is the solution? For example, how do I ensure the sound quality of audio recording when playing music? (API version 10)
217
218**Solution**
219
220The embedded 3A processing is automatically enabled for the audio stream with the **STREAM_USAGE_VOICE_COMMUNICATION** configuration. Currently, an independent switch is not provided. 3A is supported in recording scenarios. You need to configure **AudioScene** and **SourceType** to enable 3A processing in recording scenarios.
221
222**References**
223
224[AudioCapturer](../reference/apis-audio-kit/js-apis-audio.md#audiocapturer8)
225
226## How do I implement low latency audio recording?(API 11)
227
228**Solution**
229
230To implement low latency audio recording, use the C APIs provided by the **AudioCapturer** class of the **OHAudio** module. For details, see [Using OHAudio for Audio Recording (C/C++)](../media/using-ohaudio-for-recording.md).
231
232**References**
233
234[ohaudio](../reference/apis-audio-kit/_o_h_audio.md)
235
236## How do I implement real-time video stream transmission? How do I implement live broadcast? (API version 10)
237
238**Solution**
239
240Currently, the AVPlayer supports HTTP, HTTPS, and HLS for real-time video stream transmission. In the live broadcast scenario, the AVPlayer can play the data sent by the peer once it receives the live broadcast address. Stream pushing is not supported yet, which means that the Avplayer cannot use the current device for live broadcast.
241
242**References**
243
244- [Media Kit](../media/media-kit-intro.md)
245- [AVPlayer](../media/using-avplayer-for-playback.md)
246
247## How do I enable the AVPlayer to play in the background? (API version 10)
248
249**Solution**
250
251To continue background playback, the application must request a continuous task and register the AVSession with the system for unified management.
252
253**References**
254
255[Accessing AVSession](../media/avsession-access-scene.md)
256
257## Why can't a third-party application create albums? (API version 10)
258
259**Symptom**
260
261The read and write permissions of album resources are set to the system_basic level, and the APIs for creating albums are designed as system APIs. What is the reason of this design? (API version 10)
262
263**Solution**
264
265To protect the privacy of users' images and videos, any operation on these files must be notified by the users. Therefore, the read and write permissions are not granted to third-party applications. The system generates source albums based on the image and video storage sources. User-defined albums can only be created in Gallery and can be dragged to the user-defined album area.
266
267## How do I compress an image to a specified size? What are the factors affecting the size after compression? (API version 10)
268
269**Symptom**
270
271What is the relationship between the **quality** parameter in the image compression APIs and the original size and compressed size of an image? How do I set the target image size? For example, if I want to compress an image to 500 KB, how do I set the parameters?
272
273**Solution**
274
275The **quality** parameter affects the target image size for a lossy compression image format (such as JPEG), but not for a lossless compression image format (such as PNG).
276For lossy compression images, the target image size depends on the original image size, compression quality, and image content. Therefore, the current system does not support the setting of the target image size. If an application wants to specify the size, you can adjust the **quality** parameter based on the compression result, or scale the pixel map to a smaller size and then compress the pixel map.
277
278**References**
279
280- [scale](../reference/apis-image-kit/js-apis-image.md#scale9)
281- [packing](../reference/apis-image-kit/js-apis-image.md#packing)
282