HarmonyOS PhotoPickerComponent中通过onselect选中的视频uri,如何获取视频时长以及封面

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

封面参考以下文档获取:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/user-file-uri-intro-V5#ZH-CN_TOPIC_0000001884756846__

视频时长可通过如下demo实现:

import { DataType, PhotoPickerComponent, PickerController, PickerOptions } from '@ohos.file.PhotoPickerComponent';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import { image } from '@kit.ImageKit';

@Entry
@Component
struct TestPicker {
  @State message: string = 'Hello World';
  @State pickerController: PickerController = new PickerController();
  @State videoUri: string = '';
  pickerOptions: PickerOptions = new PickerOptions();
  selectUri: string[] = [];
  testUri: string = '';
  @State pixel: image.PixelMap | undefined = undefined
  private onSelect(uri: string) {
    console.log(`tag select uri is ${uri}`);

  }

  aboutToAppear(): void {
    this.pickerOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE;
    this.pickerOptions.maxSelectNumber = 9;
    this.pickerOptions.isSearchSupported = false;
    this.pickerOptions.backgroundColor = '#AAFF22'
  }

  build() {
    Column() {
      PhotoPickerComponent({
        pickerOptions: this.pickerOptions,
        onSelect: (uri: string) => {
          console.log(`tag select uri is ${uri}`);
          this.selectUri.push(uri);
          // file://media/Photo/42/VID_1717230235_037/SVID_20240601_162215_1.mp4
        },
        onDeselect: (uri: string) => console.log(`zzy deselect uri is ${uri}}`),
        pickerController: this.pickerController
      }).width('100%')
        .height(500)
      Button('1111')
        .onClick(async () => {
          this.pixel = await uriGetAssets(this.selectUri);
        })
      Image(this.pixel)
        .backgroundColor(Color.Green)
        .width(100)
        .height(100)
    }
    .height('100%')
    .width('100%')
  }
}

async function uriGetAssets(uri: string[]): Promise<undefined | image.PixelMap> {
  try {
    let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(getContext());
    let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
    predicates.equalTo('uri', uri[0]);
    let fetchOption: photoAccessHelper.FetchOptions = {
      fetchColumns: [],
      predicates: predicates
    };
    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption);
    const asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
    console.info('asset displayName: ', asset.displayName);
    console.info('asset uri: ', asset.uri);
    const pixel = await new Promise<image.PixelMap>((resolve, reject) => {
      asset.getThumbnail((err, pixelMap) => {
        if (err == undefined) {
          console.info('getThumbnail successful ' + JSON.stringify(pixelMap));
          resolve(pixelMap);
        } else {
          console.error('getThumbnail fail', err);
          reject(err);
        }
      });
    });
    return pixel;
  } catch (error){
    console.error('uriGetAssets failed with err: ' + JSON.stringify(error));
    return undefined;
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 怎么获取视频播放时长
378浏览 • 1回复 待解决
HarmonyOS如何获取视频第一帧作为封面
1074浏览 • 1回复 待解决
如何录制视频并设置最大录制时长
140浏览 • 0回复 待解决
如何视频保存到相册以及主机端
5594浏览 • 1回复 待解决
怎么把视频保存到相册以及主机端?
3862浏览 • 1回复 待解决
HarmonyOS获取相册视频宽高问题
470浏览 • 1回复 待解决
视频解码结果通过到vulkan渲染
1732浏览 • 1回复 待解决