my.chooseVideo

Call this API to shoot a video or choose a video from the album.

Sample code

See the following JavaScript sample code:

copy
my.chooseVideo({
  sourceType: ['album', 'camera'],
  maxDuration: 60,
  camera: 'back',
  success: (res) => {
    console.log(res);
  },
  fail: (error) => {
    console.log(error);
  },
  complete: () => {
    console.log('The callback function to be executed whether the request is successful or failed');
  }
});

Parameters

The incoming parameter is of the Object type with the following properties:

Property

Data type

Required

Description

sourceType

Array<String>

No

Indicates the sources of the video.

Valid values are:

  • album: Choose a video from the album on a mobile device.
  • camera: Shoot a video with the camera on a mobile device.

By default, the value is ['album','camera'].

compressed

Boolean

No

Specifies whether to compress the selected video file. For iOS, the video file is always compressed.

By default, the value is true, which means the selected video file is compressed.

maxDuration

Number

No

Indicates the maximum amount of time to shoot a video, in seconds.

By default, the value is 60, which means the maximum amount of time is 60 seconds.

camera

String

No

Indicates which camera (front or back camera) is opened when launching the camera app.

For some Android devices, this configuration is not supported by the system ROM and thus cannot take effect.

Valid values are:

  • back: The back camera is opened.
  • front: The front camera is opened.

By default, the value is back.

success

Function

No

The callback function to be executed when the request is successful. For more information, see success callback function.

fail

Function

No

The callback function to be executed when the request is failed.

complete

Function

No

The callback function to be executed when the request is completed (to be executed whether the request is successful or failed).

success callback function

The following table provides the properties in the success callback function:

Property

Type

Description

tempFilePath

String

Indicates the temporary file path of the selected video.

duration

Number

Indicates the length of the selected video.

size

Number

Indicates the size of the selected video.

height

Number

Indicates the height of the selected video.

width

Number

Indicates the width of the selected video.

Error codes

The following table provides the possible error codes and messages in the API call:

Error code

Error message

Solution

2

Invalid parameters.

Check the values in your request and provide correct parameters.

3

An unknown error occurs.

Check whether the calling process is normal. If not resolved, contact our technical support to troubleshoot this issue.

11

The operation was canceled by the user.

Normal user interaction and no further processing is required.

12

An error occurs in data processing.

Call the API again.

13

The file size exceeds the limit.

Check whether you have enough RAM.

14

No access to camera.

Check app permissions for camera.

15

No access to album.

Check app permissions for album.

FAQs

Q: How to guide users to enable permissions?

A: Call the my.showAuthGuide API to guide users to enable certain permissions.

Q: How to upload the selected video?

A: Call this API together with the my.uploadFile API to upload the selected video. See the following sample code:

copy
Page({
  uploadFile() {
    my.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success(res) {
        console.log('chooseVideo', res);
        const path = res.tempFilePath;
        console.log(path);
        my.uploadFile({
          url: 'https://httpbin.org/post',
          fileType: 'video',
          fileName: 'user-selected-video',
          filePath: path,
          formData: { extra: 'Extra information' },
          success: res => {
            console.log('success res', res);
            my.alert({ title: 'Uploaded successfully' });
          },
          fail: err => {
            my.alert({ title: 'Failed to upload', content: JSON.stringify(err) });
          },
        });
      },
    });
  },
});