HarmonyOS鸿蒙Next中Video播放file://media/Photo/路径下视频资源
HarmonyOS鸿蒙Next中Video播放file://media/Photo/路径下视频资源 描述:
我通过photoAccessHelper获取相册的图片和视频uri(Read_ImageVideo和Write_ImageVideo权限已授权),图片我能够通过Image组件加载;但是不同通过Video播放视频(且有滋滋滋的声音传出),且Video组件没有回掉onError;
测试路径:file://media/Photo/73/VID_1716262981_055/VID_20240521_114210.mp4
-
我是否还需要授权其他权限
-
是否是Video不支持此路径,若不支持是否还有其他方式可以播放
-
或者我的使用错误
Video({
src: this.testUrl,
controller: this.controller,
previewUri: $r('app.media.ic_banner_placeholder')
})
.muted(false)//是否静音
.autoPlay(true)
.loop(true)
.controls(false)//是否显示默认控制条
.width(CommonConstants.FULL_SIZE)
.height(250)
.onError(() => {
promptAction.showToast({ message: 'play error' })
})
更多关于HarmonyOS鸿蒙Next中Video播放file://media/Photo/路径下视频资源的实战教程也可以访问 https://www.itying.com/category-93-b0.html
试试
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import fs from '@ohos.file.fs';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';
import { picker } from '@kit.CoreFileKit';
@Entry
@Component
struct Index {
private controller: VideoController | undefined;
@State videoSrc: string = ''
private context = this as common.UIAbilityContext;
build() {
Column({ space: 30 }) {
Button('选择视频')
.width(200)
.height(30)
.onClick(() => {
this.PickerVideo()
})
Video({
src: this.videoSrc,
controller: this.controller
})
.width('100%')
.height(300)
}
.width('100%')
.height('100%')
}
PickerVideo() {
const photoSelectOptions = new picker.PhotoSelectOptions();
const photoViewPicker = new picker.PhotoViewPicker();
photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.VIDEO_TYPE; // 过滤选择媒体文件类型为IMAGE
photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
const fileUri = photoSelectResult.photoUris[0]
console.info('photoViewPicker.select to file succeed and uris are:' + fileUri);
this.getFileInfo(fileUri)
}).catch((err: BusinessError) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
}
async getFileInfo(filePathString: string) {
let resFile = fs.openSync(filePathString, fs.OpenMode.READ_ONLY)
const dateStr = (new Date().getTime()).toString()
// 临时文件目录
let newPath = this.context.filesDir + `${dateStr + resFile.name}`;
// 转化路径
fs.copyFileSync(resFile.fd, newPath);
// 新的路径
let realUri = 'file://' + newPath;
this.videoSrc = realUri
console.log(this.videoSrc)
}
}
更多关于HarmonyOS鸿蒙Next中Video播放file://media/Photo/路径下视频资源的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,播放file:///media/Photo/
路径下的视频资源,可以使用VideoPlayer
组件。首先,确保视频文件路径正确且权限已配置。在config.json
中声明ohos.permission.READ_MEDIA
权限。然后,在代码中创建VideoPlayer
实例,设置视频源路径为file:///media/Photo/your_video.mp4
,调用prepare()
方法准备播放,最后调用play()
方法开始播放。如果需要控制播放状态,可以使用pause()
、stop()
等方法。确保UI线程与播放器操作分离,避免阻塞。
在HarmonyOS鸿蒙Next中,您可以使用VideoPlayer
组件来播放本地视频文件。假设您已经获取了视频文件的路径file:///media/Photo/your_video.mp4
,可以通过以下代码实现视频播放:
VideoPlayer videoPlayer = new VideoPlayer(context);
videoPlayer.setVideoURI(Uri.parse("file:///media/Photo/your_video.mp4"));
videoPlayer.start();
确保应用已获得读取存储权限,路径正确且文件存在。