HarmonyOS 鸿蒙Next中相册中的视频怎么过滤掉90秒以上的视频

HarmonyOS 鸿蒙Next中相册中的视频怎么过滤掉90秒以上的视频 使用PhotoViewPicker如何过滤掉90秒以上的视频?

3 回复

开发者你好,可以参考以下方案:

【解决方案】 使用PhotoAccessHelper拉起相册界面时,可以配置图库选择选项,API19提供了FileSizeFilter过滤媒体文件大小,VideoDurationFilter过滤媒体文件视频时长。

参考示例代码:

import photoAccessHelper from '@ohos.file.photoAccessHelper';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct PhotoViewPickerPage {
  build() {
    RelativeContainer() {
      Button('过滤大于90秒的视频')
        .id('PhotoViewPickerPageHelloWorld')
        .fontSize($r('app.float.page_text_font_size'))
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          let options: photoAccessHelper.PhotoSelectOptions = {
            videoDurationFilter: {
              filterOperator: photoAccessHelper.FilterOperator.LESS_THAN,
              videoDuration: 90000
            },
            MIMEType: photoAccessHelper.PhotoViewMIMETypes.VIDEO_TYPE
          };
          let photoPicker = new photoAccessHelper.PhotoViewPicker();
          photoPicker.select(options).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
            console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' +
            JSON.stringify(PhotoSelectResult));
          }).catch((err: BusinessError) => {
            console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
          });
        })
    }
    .height('100%')
    .width('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next中相册中的视频怎么过滤掉90秒以上的视频的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,可以通过MediaLibrary接口查询相册视频。使用MediaFetchOptions设置查询条件,通过添加媒体时长筛选参数,指定duration参数小于或等于90000毫秒(即90秒),即可过滤出90秒以下的视频。具体实现需调用媒体库相关API完成数据检索。

在HarmonyOS Next中,可以通过PhotoViewPickerPhotoSelectOptions设置视频选择条件。使用predicatesduration参数来过滤时长超过90秒的视频:

import photoAccessHelper from '@ohos.file.photoAccessHelper';

let predicates: photoAccessHelper.PhotoKeys = {
  duration: photoAccessHelper.PhotoKeys.DURATION, // 视频时长键
  value: [0, 90], // 时长范围:0到90秒
  operation: photoAccessHelper.OperationType.BETWEEN // 区间匹配
};

let options: photoAccessHelper.PhotoSelectOptions = {
  selectCount: 1,
  MIMEType: photoAccessHelper.PhotoViewMIMETypes.VIDEO_TYPE, // 限制为视频类型
  predicates: [predicates]
};

let photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(options).then((result) => {
  console.info('PhotoViewPicker.select successfully, result: ' + JSON.stringify(result));
}).catch((error) => {
  console.error('PhotoViewPicker.select failed with error: ' + error);
});

关键点:

  1. MIMEType设为VIDEO_TYPE确保只选择视频
  2. predicatesdurationvalue设为[0, 90],配合BETWEEN操作符实现时长过滤
  3. 此配置将只显示时长在90秒及以下的视频文件

注意实际使用时需处理权限申请和错误回调。

回到顶部