has.chooseMedia(Object object)
拍摄或从手机相册中选择图片或视频。
起始版本: 1.0.0
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 默认值 | 必填 | 描述 |
|---|---|---|---|---|
| count | number | 9 | 否 | 最多可以选择的图片张数,最大不能超过20。 |
| mediaType | Array<string> | ['image', 'video'] | 否 | 文件类型。 - image :只能拍摄图片或从相册选择图片。 - video:只能拍摄视频或从相册选择视频。 - mix:可同时选择图片和视频。 |
| sizeType | Array<string> | ['original', 'compressed'] | 否 | 所选的图片的尺寸。 - original:原图。 - compressed:压缩图。 |
| sourceType | Array<string> | ['album', 'camera'] | 否 | 选择图片的来源。 - album:从相册选图。 - camera:使用相机拍摄。 |
| maxDuration | number | 10 | 否 | 拍摄视频最长拍摄时间,单位:s。时间范围为3s至60s之间。仅在sourceType为camera时生效。 |
| camera | string | 'back' | 否 | 仅在sourceType为camera时生效,使用前置或后置摄像头。仅允许: - back:后置摄像头。 - front:前置摄像头。 |
| success | function | - | 否 | 接口调用成功的回调函数。 |
| fail | function | - | 否 | 接口调用失败的回调函数。 |
| complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
| 参数 | 类型 | 描述 |
|---|---|---|
| tempFiles | Array<Object> | 图片的本地临时文件列表。 |
| type | string | 文件类型,有效值有image 、video、mix。 |
tempFiles:
| 属性 | 类型 | 描述 |
|---|---|---|
| tempFilePath | string | 本地临时文件路径(本地路径)。 |
| size | number | 本地临时文件大小,单位:B。 |
| duration | number | 视频时长。 在ASCF运行时2.0.1版本之前,单位:ms;2.0.1及以后版本,单位:s。 |
| height | number | 视频的高度。 |
| width | number | 视频的宽度。 |
| thumbTempFilePath | string | 视频缩略图临时文件路径。 |
| fileType | string | 文件类型。 - image:图片。 - video:视频。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
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
创建video上下文VideoContext对象。
起始版本: 1.0.0
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| id | string | 是 | video组件的id。 |
| this | Object | 否 | 在自定义组件下,通过当前组件实例的this,来操作组件内video组件。 起始版本: 1.0.16 |
返回值:
返回VideoContext对象。
示例:
let videoContext = has.createVideoContext('myVideo'); VideoContext.play()
播放视频。
起始版本: 1.0.0
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. 更多错误信息参考:Media错误码 |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.play(); VideoContext.pause()
暂停播放视频。
起始版本: 1.0.0
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.pause(); VideoContext.stop()
停止播放视频。
起始版本: 1.0.4
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.stop(); VideoContext.seek(number position)
视频跳转到指定位置。
起始版本: 1.0.4
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| position | number | 是 | 跳转到的位置,单位:s。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | videoController is undefined. |
| 102 | position is invalid. |
| 70004501 | video cannot seek in current state. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.seek(5); VideoContext.playbackRate(number rate)
设置视频倍速播放。
起始版本: 1.0.4
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| rate | number | 是 | 播放倍速。支持的播放倍速有:0.125、0.25、0.5、0.75、1、1.25、1.5、1.75、2,部分投屏设备不支持全量的播放倍速。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.playbackRate(0.5); VideoContext.requestFullScreen(Object object)
进入全屏。
起始版本: 1.0.4
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| direction | number | 否 | 设置全屏时视频的方向,不指定则根据宽高比自动判断。 0:正常竖向。 90:屏幕逆时针90°。 -90:屏幕顺时针90°。 起始版本: 1.0.16 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | videoController is undefined. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.requestFullScreen({
direction: 0
}); VideoContext.exitFullScreen()
退出全屏。
起始版本: 1.0.4
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | videoController is undefined. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.exitFullScreen(); VideoContext.requestBackgroundPlayback()
进入后台音频播放模式。
起始版本: 1.0.16
需要权限: 开启后台音频播放,需要如下配置:
在module.json5中声明backgroundModes配置项。
"module": {
"abilities": [
{
"backgroundModes": [
"audioPlayback"
]
}
]
} 在app.json中配置requiredBackgroundModes属性。
"requiredBackgroundModes": ["audio"]
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | videoController is undefined. |
| 70004701 | 1. background audio is not grant. 2. video is casting. 3. video cannot enter background playback in current state. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.requestBackgroundPlayback(); VideoContext.exitBackgroundPlayback()
退出后台音频播放模式。
起始版本: 1.0.16
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | 1. videoController is undefined. 2. AVPlayer is not initialized. |
| 70004801 | 1. background audio is not grant. 2. video is casting. |
示例:
const videoContext = has.createVideoContext('myVideo');
videoContext.exitBackgroundPlayback(); has.saveVideoToPhotosAlbum(Object object)
保存视频到系统相册。
起始版本: 1.0.4
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| filePath | string | 是 | 视频文件路径,可以是临时文件路径,也可以是永久文件路径(本地路径)。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
has.saveVideoToPhotosAlbum({
filePath: 'internal://tmp/xxx.mp4', // 此处仅为样例,请开发者更换为可用视频资源地址。
success: () => {
console.info('saveVideoToPhotosAlbum success');
},
fail: (err) => {
console.error('saveVideoToPhotosAlbum fail', err);
},
complete: (res) => {
console.info('saveVideoToPhotosAlbum complete', res);
}
}); has.getVideoInfo(Object object)
获取视频详细信息。
起始版本: 1.0.4
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| src | string | 是 | 视频文件路径,可以是临时文件路径也可以是永久文件路径。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
| 参数 | 类型 | 描述 |
|---|---|---|
| orientation | string | 画面方向,返回以下值: - up:默认。 - down:180°旋转。 - left:逆时针旋转90°。 - right:顺时针旋转90°。 |
| type | string | 视频格式。 |
| size | number | 视频大小,单位:kB。 |
| duration | number | 视频时长。 在ASCF运行时2.0.1版本之前,单位:ms;2.0.1及以后版本,单位:s。 |
| height | number | 视频的高度,单位:px。 |
| width | number | 视频的宽度,单位:px。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
has.getVideoInfo({
src: 'internal://tmp/xxx.mp4', // 此处仅为样例,请开发者更换为可用视频资源地址。
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)
拍摄视频或从手机相册中选视频。
起始版本: 1.0.4
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 默认值 | 必填 | 描述 |
|---|---|---|---|---|
| sourceType | Array<string> | ['album', 'camera'] | 否 | 视频选择的来源。 - album:从相册选择视频。 - camera:使用相机拍摄视频。 |
| maxDuration | number | 60 | 否 | 拍摄视频最长拍摄时间,单位:s。时间范围为3s至60s之间。仅在sourceType为camera时生效。 |
| camera | string | 'back' | 否 | 仅在sourceType为camera时生效,使用前置或后置摄像头。仅允许: - back:后置摄像头。 - front:前置摄像头。 |
| success | function | - | 否 | 接口调用成功的回调函数。 |
| fail | function | - | 否 | 接口调用失败的回调函数。 |
| complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
| 参数 | 类型 | 描述 |
|---|---|---|
| tempFilePath | string | 选定视频的临时文件路径(本地路径)。 |
| duration | number | 视频时长。 在ASCF运行时2.0.1版本之前,单位:ms;2.0.1及以后版本,单位:s。 |
| size | number | 视频大小,单位:kB。 |
| height | number | 返回选定视频的高度。 |
| width | number | 返回选定视频的宽度。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
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);
}
});