HarmonyOS鸿蒙Next中m3u8视频文件有几百个片段,让用户选几百个片段不合理,有没有workaround的方法来解决这个问题
HarmonyOS鸿蒙Next中m3u8视频文件有几百个片段,让用户选几百个片段不合理,有没有workaround的方法来解决这个问题 【问题描述】:m3u8视频文件有几百个片段,让用户选几百个片段不合理,有没有workaround的方法来解决这个问题
【问题现象】:代码逻辑是根据 m3u8 路径推断视频文件夹,调起系统文件夹选择器,选择成功后读取并输出文件列表日志,调起系统文件夹选择器,并预定位到推断目录,但会报错The default system capabilities of devices phone, tablet do not include SystemCapability. FileManagement. UserFileService. FolderSelection. Configure the capabilities in syscap. json. 后续发现DocumentSelectMode仅支持PC/2in1并不支持phone,在安卓上可以通过m3u8文件,找到对应的目录,然后拉起系统文件管理器,选中这个目录,拷贝文件到私有目录,然后基于私有目录下的文件路径,使用ffmpeg进行转换,鸿蒙这边有什么方案吗
【版本信息】:未涉及
【复现代码】:未涉及
【尝试解决方案】:未涉及
更多关于HarmonyOS鸿蒙Next中m3u8视频文件有几百个片段,让用户选几百个片段不合理,有没有workaround的方法来解决这个问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
尊敬的开发者您好,您可参考以下方案:
可以全选到m3u8格式文件
示例代码:
import { picker } from "@kit.CoreFileKit";
import { common } from "@kit.AbilityKit";
import { BusinessError } from "@kit.BasicServicesKit";
@Entry
@Component
struct Index {
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
build() {
Column({space:6}){
Text("选择m3u8文件")
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
try {
let documentSelectOptions = new picker.DocumentSelectOptions();
// 指定选择目录的URI(根据 m3u8 路径推断视频文件夹)
documentSelectOptions.defaultFilePathUri = 'file://docs/storage/Users/currentUser/Download/test';
documentSelectOptions.fileSuffixFilters = ['.m3u8'];
let documentPicker = new picker.DocumentViewPicker(this.context);
documentPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => {
console.info('DocumentViewPicker.select successfully, documentSelectResult uri: ' + JSON.stringify(documentSelectResult));
}).catch((err: BusinessError) => {
console.error(`DocumentViewPicker.select failed with err, code is: ${err.code}, message is: ${err.message}`);
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`DocumentViewPicker failed with err, code is: ${err.code}, message is: ${err.message}`);
}
})
}
}
}
更多关于HarmonyOS鸿蒙Next中m3u8视频文件有几百个片段,让用户选几百个片段不合理,有没有workaround的方法来解决这个问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
开发者您好,API23及以上版本可以进行全选。
API23以下版本您可尝试将目标m3u8文件做成一个压缩包放在公共目录下,先选择这个压缩包,然后拷贝到应用沙箱里再转换拼接播放。
Phone/Tablet 上不建议按“选择 m3u8 后反推出目录,再拿目录权限读取几百个分片”的方式做。你看到的 syscap 报错说明当前设备不支持 FolderSelection,而 DocumentViewPicker 选中一个 m3u8 文件,只会给这个文件 URI 的临时授权,不会自动授权同目录下的 ts/key 等文件;defaultFilePathUri 也只是定位/建议路径,不等于目录授权。
可行的 workaround 通常有几类:
- 导入前把
m3u8 + ts/key打成 zip 或自定义包,用户只选择一个文件,App 解压到沙箱后再用 ffmpeg 处理。 - 如果分片来自你自己的服务端或下载流程,由 App 直接下载到应用沙箱,自己维护 m3u8 与分片索引,后续处理都走沙箱路径。
- 使用多选作为兜底:
DocumentSelectOptions.maxSelectNumber可以提高选择数量上限,但几百个分片体验很差,也容易选漏。 - 服务端或导入前先合并/转码成单个 mp4,再让用户选择单文件。
- 只有 PC/2in1 场景再考虑目录选择能力;移动端不要依赖
FolderSelection。
所以移动端最稳的是“单包导入”或“App 自己下载到沙箱”,而不是通过已选文件去获取它所在目录的读权限。
在手机端,可选择DocumentViewPicker让用户选取打包好的.zip文件,应用内部将其解压到私有目录后再处理。
操作流程:
- 引导用户将包含m3u8及所有.ts片段的文件夹打包为一个.zip。
- 应用通过
DocumentViewPicker选中该.zip,获得uri后读取并解压到应用沙箱(如context.filesDir)。 - 解压得到的目录结构与原视频目录一致,直接定位m3u8文件即可使用ffmpeg转换。
这样用户只需操作一个文件,避免选择数百个片段。


