has.chooseMedia(Object object)
Captures images or videos using the camera or selects them from the phone's album.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| count | number | 9 | No | Maximum number of images that can be selected. The maximum value is 20. |
| mediaType | Array<string> | ['image', 'video'] | No | File type. - image: You can only capture images or select images from the album. - video: You can only capture videos or select videos from the album. - mix: You can select both images and videos from the album. |
| sizeType | Array<string> | ['original', 'compressed'] | No | Size of the selected image. - original: original size. - compressed: compressed size. |
| sourceType | Array<string> | ['album', 'camera'] | No | Image source. - album: selected from the album. - camera: captured using the camera. |
| maxDuration | number | 10 | No | Maximum duration for video recording, in seconds. Value range: 3s to 60s. This parameter is valid only when sourceType is set to camera. |
| camera | string | 'back' | No | Whether the front or rear camera is used. This parameter is valid only when sourceType is set to camera. Options: - back: rear camera. - front: front camera. |
| success | function | - | No | Callback triggered when the API call is successful. |
| fail | function | - | No | Callback triggered when the API call fails. |
| complete | function | - | No | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| tempFiles | Array<Object> | List of local temporary image files. |
| type | string | File type, which can be image, video, and mix. |
tempFiles
| Attribute | Type | Description |
|---|---|---|
| tempFilePath | string | Local temporary file path (local path). |
| size | number | Size of a local temporary file, in bytes. |
| duration | number | Video duration. In versions earlier than ASCF 2.0.1, the unit is ms. In ASCF 2.0.1 and later versions, the unit is s. |
| height | number | Video height. |
| width | number | Video width. |
| thumbTempFilePath | string | Local temporary file path for the video thumbnail. |
| fileType | string | File type. - image: images. - video: videos. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | 1. context is undefined. 2. applicationContext is undefined. |
| 102 | 1. chooseMedia, invalid input params. 2. parseSelectNumber, invalid input params. 3. chooseFromCamera, invalid input params. 4. getProfile, invalid input params. |
| 70001401 | chooseMedia, invoke failed. |
| 70001402 | chooseFromCamera, open camera error. |
| 70001404 | chooseFromPhotos, choose photos failed. |
Example:
has.chooseMedia({
count: 2,
mediaType: ['image', 'video'],
sourceType: ['album', 'camera'],
maxDuration: 10,
sizeType: ['original', 'compressed'],
camera: 'front',
success: (res) => {
console.info('chooseMedia success', res);
},
fail: (err) => {
console.error('chooseMedia fail', err);
},
complete: (res) => {
console.info('chooseMedia complete', res);
}
}); has.createVideoContext(String id): VideoContext
Creates a video context object.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| id | string | Yes | ID of the video component. |
| this | Object | No | this of the current component instance operates the component video under custom components. Since: 1.0.16 |
Returns:
VideoContext object.
Example:
let videoContext = has.createVideoContext('myVideo'); VideoContext.play()
Starts video playback.
Since: 1.0.0
Error code information
| Error Code | Error Message |
|---|---|
| 101 | 1. videoController is undefined. 2. cast device is not connected. 3. AVPlayer is not initialized. |
| 70004201 | video cannot play in current state. |
| 70005601 | video cannot setMuted in current state. |
| 70004901 | command is not support. For details, see Media Error Codes. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.play(); VideoContext.pause()
Pauses video playback.
Since: 1.0.0
Error code information
| Error Code | Error Message |
|---|---|
| 101 | 1. videoController is undefined. 2. cast device is not connected. 3. AVPlayer is not initialized. 4. params is invalid. 5. background audio is not grant. 6. the avPlayer is already created a session. |
| 103 | has no permission. |
| 70004301 | 1. video cannot pause in current state. 2. video cast cannot pause in current state. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.pause(); VideoContext.stop()
Stops video playback.
Since: 1.0.4
Error code information
| Error Code | Error Message |
|---|---|
| 101 | 1. videoController is undefined. 2. AVPlayer is not initialized. 3. cast device is not connected. |
| 70004301 | video cast cannot pause in current state. |
| 70004401 | video cannot stop in current state. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.stop(); VideoContext.seek(number position)
Seeks to a specified playback position.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| position | number | Yes | Target position, in seconds. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | videoController is undefined. |
| 102 | position is invalid. |
| 70004501 | video cannot seek in current state. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.seek(5); VideoContext.playbackRate(number rate)
Sets the video playback rate.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| rate | number | Yes | Playback rate. The supported rates are 0.125, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, and 2. Some screen casting devices do not support all playback rates. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | videoController is undefined. |
| 102 | rate is invalid. |
| 70004601 | 1. rate is only support: 0.125 0.25 0.5 0.75 1 1.25 1.5 1.75 2. 2. video cannot playbackRate in current state. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.playbackRate(0.5); VideoContext.requestFullScreen(Object object)
Transitions to full-screen mode.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| direction | number | No | Video orientation in full-screen mode. If not specified, the orientation is automatically determined by the aspect ratio. 0: upright portrait. 90: 90° counterclockwise. -90: 90° clockwise. Since: 1.0.16 |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | videoController is undefined. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.requestFullScreen({
direction: 0
}); VideoContext.exitFullScreen()
Exits full-screen mode.
Since: 1.0.4
Error code information
| Error Code | Error Message |
|---|---|
| 101 | videoController is undefined. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.exitFullScreen(); VideoContext.requestBackgroundPlayback()
Requests background audio playback.
Since: 1.0.16
Required permissions: To enable background audio playback, perform the following operations:
Declare the ohos.permission.KEEP_BACKGROUND_RUNNING permission in module.json5.
Declare the backgroundModes configuration item in module.json5.
"module": {
"abilities": [
{
"backgroundModes": [
"audioPlayback"
]
}
]
} Configure the requiredBackgroundModes attribute in app.json.
"requiredBackgroundModes": ["audio"]
Error code information
| Error Code | Error Message |
|---|---|
| 101 | videoController is undefined. |
| 70004701 | 1. background audio is not grant. 2. video is casting. 3. video cannot enter background playback in current state. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.requestBackgroundPlayback(); VideoContext.exitBackgroundPlayback()
Exits background audio playback.
Since: 1.0.16
Error code information
| Error Code | Error Message |
|---|---|
| 101 | 1. videoController is undefined. 2. AVPlayer is not initialized. |
| 70004801 | 1. background audio is not grant. 2. video is casting. |
Example:
const videoContext = has.createVideoContext('myVideo');
videoContext.exitBackgroundPlayback(); has.saveVideoToPhotosAlbum(Object object)
Saves a video to the system album.
Since: 1.0.4
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Video file path, which can be a temporary file path or a permanent file path (local path). |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | 1. context is undefined. 2. applicationContext is undefined. |
| 70001701 | video is saving. |
| 70001702 | 1. file path is invalid. 2. uiContext is null. 3. no need save video. 4. save video to album error. 5. saveVideoToPhotosAlbum error. |
Example:
has.saveVideoToPhotosAlbum({
filePath: 'internal://tmp/xxx.mp4', // Replace the example with the actual video resource address.
success: () => {
console.info('saveVideoToPhotosAlbum success');
},
fail: (err) => {
console.error('saveVideoToPhotosAlbum fail', err);
},
complete: (res) => {
console.info('saveVideoToPhotosAlbum complete', res);
}
}); has.getVideoInfo(Object object)
Obtains the video information.
Since: 1.0.4
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| src | string | Yes | Video file path, which can be a temporary file path or a permanent file path. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| orientation | string | Image orientation. Options: - up: default. - down: Image rotated by 180°. - left: Image rotated counterclockwise by 90°. - right: Image rotated clockwise by 90°. |
| type | string | Video format. |
| size | number | Video size, in KB. |
| duration | number | Video duration. In versions earlier than ASCF 2.0.1, the unit is ms. In ASCF 2.0.1 and later versions, the unit is s. |
| height | number | Video height, in px. |
| width | number | Video width, in px. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | 1. applicationContext is undefined. 2. context is undefined. |
| 102 | getVideoInfo, file is not exist or no permission. |
| 70001601 | 1. getVideoInfo, Media.AVMetadataExtractor not support. 2. getVideoInfo error. 3. getVideoInfo, file path is invalid. |
Example:
has.getVideoInfo({
src: 'internal://tmp/xxx.mp4', // Replace the example with the actual video resource address.
success: (res) => {
console.info('getVideoInfo success', res);
},
fail: (err) => {
console.error('getVideoInfo fail', err);
},
complete: (res) => {
console.info('getVideoInfo complete', res);
}
}); has.chooseVideo(Object object)
Captures videos using the camera or selects them from the phone's album.
Since: 1.0.4
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| sourceType | Array<string> | ['album', 'camera'] | No | Video source. - album: selected from the album. - camera: captured using the camera. |
| maxDuration | number | 60 | No | Maximum duration for video recording, in seconds. Value range: 3s to 60s. This parameter is valid only when sourceType is set to camera. |
| camera | string | 'back' | No | Whether the front or rear camera is used. This parameter is valid only when sourceType is set to camera. Options: - back: rear camera. - front: front camera. |
| success | function | - | No | Callback triggered when the API call is successful. |
| fail | function | - | No | Callback triggered when the API call fails. |
| complete | function | - | No | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| tempFilePath | string | Temporary file path (local path) of the selected video. |
| duration | number | Video duration. In versions earlier than ASCF 2.0.1, the unit is ms. In ASCF 2.0.1 and later versions, the unit is s. |
| size | number | Video size, in KB. |
| height | number | Video height. |
| width | number | Video width. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | context is undefined. |
| 102 | 1. chooseVideo, invalid input params. 2. chooseVideoFromPhotos, invalid input params. 3. chooseVideoFromCamera, invalid input params. |
| 70001501 | chooseVideo, invoke failed. |
| 70001502 | 1. chooseVideoFromCamera, invalid input params. 2. chooseVideoFromCamera, open camera error. 3. chooseVideoFromCamera, open camera failed. |
| 70001503 | chooseVideoFromPhotos, choose photos failed. |
Example:
has.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 60,
camera: 'back',
success: (res) => {
console.info('chooseVideo success', res);
},
fail: (err) => {
console.error('chooseVideo fail', err);
},
complete: (res) => {
console.info('chooseVideo complete', res);
}
});