参考代码://申明缩略图时间点与视频帧的对应关系。 let queryOption = media.AVImageQueryOptions.AV_IMAGE_QUERY_CLOSEST_SYNC //缩略图的格式参数。 let param: media.PixelMapParams = { width: 300, height: 300, } async aboutToAppear(): Promise<void> { // 创建AVImageGenerator对象 avImageGenerator = await media.createAVImageGenerator() // 设置fdSrc avImageGenerator.fdSrc = await getContext(this).resourceManager.getRawFd('VID_1713928724_004.mp4'); } async testFetchFrameByTime(i: number) { // 获取缩略图(promise模式) this.pixelMap = await avImageGenerator.fetchFrameByTime(i, queryOption, param) } //间隔1秒获取视频缩略图 onClick(() => { i = i + 1000000; Logger.info("current i is :" + i); this.testFetchFrameByTime(i) }) Image(this.pixelMap).width(300).height('30%')// .syncLoad(true) .margin({ top: 20 }) //核心思路 showPicker() { // 创建图库选择器对象实例 const photoViewPicker = new photoAccessHelper.PhotoViewPicker(); // 图库选项配置,类型与数量 let selectOptions = new photoAccessHelper.PhotoSelectOptions(); selectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.VIDEO_TYPE; selectOptions.maxSelectNumber = 1; //调用select()接口拉起图库界面进行文件选择,文件选择成功后,返回PhotoSelectResult结果集 photoViewPicker.select(selectOptions).then((photoSelectResult: picker.PhotoSelectResult) => { //用一个全局变量存储返回的uri if (photoSelectResult.photoUris?.[0]) { this.testFetchFrameByTime(photoSelectResult.photoUris[0]) } }).catch((err: BusinessError) => { console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); }) } // 获取缩略图 async testFetchFrameByTime(filePath: string) { // 创建AVImageGenerator对象 let avImageGenerator: media.AVImageGenerator = await media.createAVImageGenerator() let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); let avFileDescriptor: media.AVFileDescriptor = { fd: file.fd }; avImageGenerator.fdSrc = avFileDescriptor; // 初始化入参 let timeUs = 0 let queryOption = media.AVImageQueryOptions.AV_IMAGE_QUERY_NEXT_SYNC let param: media.PixelMapParams = { width : 300, height : 400, } // 获取缩略图(promise模式) this.pixelMap = await avImageGenerator.fetchFrameByTime(timeUs, queryOption, param) // 释放资源(promise模式) avImageGenerator.release() console.info(`release success.`) fs.closeSync(file); } }
参考代码: