You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决just_audio加载应用文档目录音频的Unsupported URL异常

解决just_audio加载应用文档目录音频文件的问题

以下是可行的解决方案,针对你遇到的unsupported URL错误逐一排查解决:

  • 先确认文件真实存在
    加载前先验证文件是否成功保存到目标路径,避免因路径错误或保存失败导致的问题:

    final targetFile = File(yourSavedFilePath);
    if (!await targetFile.exists()) {
      throw Exception("目标文件不存在:${targetFile.path}");
    }
    
  • 优先使用AudioSource.file()加载本地文件
    just_audio针对本地文件提供了更直接的AudioSource.file()方法,比Uri解析更适配平台,能避免多数路径格式问题:

    final audioSource = AudioSource.file(
      yourSavedFilePath,
      tag: MediaItem(
        id: yourSavedFilePath, // 用文件路径作为MediaItem的id,保证后台播放正常识别
        album: "",
        title: "",
      ),
    );
    
    await player.setAudioSource(audioSource);
    
  • 若必须用Uri方式,生成正确的文件Uri
    如果坚持使用AudioSource.uri(),需确保生成符合平台要求的Uri,不要手动拼接字符串:

    final fileUri = Uri.file(yourSavedFilePath);
    final audioSource = AudioSource.uri(
      fileUri,
      tag: MediaItem(
        id: fileUri.toString(),
        album: "",
        title: "",
      ),
    );
    await player.setAudioSource(audioSource);
    
  • 检查平台兼容性(iOS注意点)
    iOS的应用沙箱路径格式特殊,Uri.file()会自动处理成file://开头的合法格式,不要自行修改路径字符串,否则容易触发unsupported URL错误。

内容的提问来源于stack exchange,提问作者Zeddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:15:02