HarmonyOS Video组件支持读取播放相册内的视频吗?

看文档上说Video支持file:///data/storage路径前缀的字符串,用于读取应用沙箱路径内的资源,现在有一个从相册中选择完视频然后播放的功能,相册选择的视频路径是file://media/Photo/21/VID_1715829569_000/SVID_20240516_111749_1.mp4,目前无法正常播放。

HarmonyOS
2024-11-12 10:39:30
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

video组件只能播放沙箱Resources里的文件,这边把选择的文件copy到沙箱路径中试试。您可以参照以下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-11-12 17:36:32
相关问题
video player播放在线视频失败
10014浏览 • 1回复 待解决
HarmonyOS Media kit支持常见云视频播放
366浏览 • 1回复 待解决
HarmonyOS 有RN Video组件封装
306浏览 • 1回复 待解决