HarmonyOS Video组件如何播放图库内的某一个视频?

​1、先通过photoPicker.select打开图库,选择完某一条视频之后,拿到了mp4文件地址(file://media/Photo/26/VID_1715666488_001/VID_20240514_135954.mp4)

2、通过Video组件加载本地视频,无法播放​。

Video({  
  src:'file://media/Photo/26/VID_1715666488_001/VID_20240514_135954.mp4',  
  controller: this.controller  
})

请提供一个当前场景的代码示例或者demo,感谢!

HarmonyOS
2024-10-29 10:42:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

​video组件只能播放沙箱Resources里的文件,您这边把选择的文件copy到沙箱路径中试试。

参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-media-components-video-V5#videooptions对象说明

您可以参照以下demo可以播放:​

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 = getContext(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)  
  }  
}
分享
微博
QQ
微信
回复
2024-10-29 16:47:19
相关问题
Scroll中点击某一个层图片移动到顶端
864浏览 • 1回复 待解决
求告知如何全屏播放一个视频
506浏览 • 1回复 待解决