HarmonyOS鸿蒙Next中AVPlayer怎么实现视频内截图功能?
HarmonyOS鸿蒙Next中AVPlayer怎么实现视频内截图功能? 视频播放器场景。用户点击按钮,软件主动截取视频某一帧图片(不是全局截屏),最后弹出保存弹窗让用户来保存。
3 回复
演示图:

关键截图函数:
import { image } from '@kit.ImageKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { fileIo, fileUri } from '@kit.CoreFileKit';
captureScreen = () => {
// 1. 截屏 → 沙箱临时文件
const pixelMap = image.createPixelMapFromSurfaceSync(this.player.surfaceID);
const packOpts: image.PackingOption = { format: 'image/png', quality: 100 };
const imagePackerApi = image.createImagePacker();
const tempPath =this.context.tempDir + '/screenshot.png';
const tempFile = fileIo.openSync(tempPath, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
let srcUri: string;
imagePackerApi.packToFile(pixelMap, tempFile.fd, packOpts)
.then(() => imagePackerApi.release())
.then(() => {
fileIo.closeSync(tempFile);
// 2. 用系统对话框保存到相册
srcUri = fileUri.getUriFromPath(tempPath);
const helper = photoAccessHelper.getPhotoAccessHelper(this.context);
return helper.showAssetsCreationDialog(
[srcUri],
[{ photoType: photoAccessHelper.PhotoType.IMAGE, fileNameExtension: 'png' }]
);
})
.then(dstUris => {
// 3. 拷贝到用户选择的相册位置
if (dstUris.length) {
const src = fileIo.openSync(srcUri, fileIo.OpenMode.READ_ONLY);
const dst = fileIo.openSync(dstUris[0], fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
fileIo.copyFileSync(src.fd, dst.fd);
fileIo.closeSync(src);
fileIo.closeSync(dst);
}
})
.catch((err: BusinessError) => {
console.error(`captureScreen failed: ${err.code}, ${err.message}`);
});
}
更多关于HarmonyOS鸿蒙Next中AVPlayer怎么实现视频内截图功能?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


