HarmonyOS 鸿蒙Next如何获取视频的总时长?
HarmonyOS 鸿蒙Next如何获取视频的总时长?
我的需求:
从
let photoPicker = new picker.PhotoViewPicker();
选择图片和视频,然后上传文件,其中视频类型文件需要获取其视频总时长。 请问如何获取视频的总时长?
更多关于HarmonyOS 鸿蒙Next如何获取视频的总时长?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
可通过如下demo实现:
import { DataType, PhotoPickerComponent, PickerController, PickerOptions } from '[@ohos](/user/ohos).file.PhotoPickerComponent';
import dataSharePredicates from '[@ohos](/user/ohos).data.dataSharePredicates';
import photoAccessHelper from '[@ohos](/user/ohos).file.photoAccessHelper';
import { image } from '[@kit](/user/kit).ImageKit';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct TestPicker {
[@State](/user/State) message: string = 'Hello World';
[@State](/user/State) pickerController: PickerController = new PickerController();
[@State](/user/State) videoUri: string = '';
pickerOptions: PickerOptions = new PickerOptions();
selectUri: string[] = [];
testUri: string = '';
[@State](/user/State) pixel: image.PixelMap | undefined = undefined
private onSelect(uri: string) {
console.log(`zzy 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(`zzy 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;
}
}
更多关于HarmonyOS 鸿蒙Next如何获取视频的总时长?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS 鸿蒙Next系统中,获取视频的总时长通常需要使用媒体框架(Media Framework)提供的API。以下是一个基本的获取视频总时长的方法:
-
创建播放器实例:首先,需要创建一个媒体播放器实例,该实例可以加载并播放视频文件。
-
加载视频文件:使用播放器实例加载目标视频文件。
-
获取媒体信息:加载视频后,可以通过播放器实例获取视频的媒体信息,其中包括总时长。
示例代码(伪代码,具体实现需参考HarmonyOS SDK文档):
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setSource(videoUri); // videoUri为视频文件的URI
mediaPlayer.prepare(); // 准备播放
MediaInfo mediaInfo = mediaPlayer.getMediaInfo();
long duration = mediaInfo.getDuration(); // 获取视频总时长,单位为毫秒
// duration即为视频的总时长
注意:
MediaPlayer
是用于播放媒体文件的类。setSource
方法用于设置媒体文件的来源。prepare
方法用于准备媒体文件,必须在获取媒体信息之前调用。getMediaInfo
方法返回媒体文件的信息,包括时长、编码格式等。getDuration
方法返回媒体文件的总时长,单位为毫秒。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html