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

Flutter音频加载失败求助:Unable to load asset错误排查

Flutter音频资源加载失败排查方案
  • 修复路径重复问题
    错误提示的路径是assets/assets/audio/ontheroad.mp3,明显重复了assets/前缀。因为pubspec.yaml已配置assets/audio/,使用AssetSource时不需要再加根目录的assets/,直接用audio/ontheroad.mp3即可。你需要把PlaylistProvider中Song对象的音频路径从assets/audio/ontheroad.mp3改成audio/ontheroad.mp3。

  • 校验pubspec.yaml配置格式
    确保assets配置的缩进正确(必须是2个空格,作为flutter:的子项),示例:

    flutter:
      assets:
        - assets/audio/
        - assets/image/
    

    修改配置后必须执行flutter pub get刷新资源,否则新配置不会生效。

  • 确认文件实际存在
    检查项目目录里是否真的有assets/audio/ontheroad.mp3文件,注意文件名大小写敏感(比如OnTheRoad.mp3和ontheroad.mp3是两个不同文件),同时确认文件后缀、拼写没有错误。

  • 清理缓存重启项目
    缓存残留可能导致旧路径生效,执行以下步骤:

    • 停止当前运行的项目
    • 执行flutter clean清理缓存
    • 重新执行flutter pub get
    • 重启项目
  • 直接测试路径播放
    暂时绕过PlaylistProvider,直接写死路径测试:

    _audioPlayer.play(AssetSource('audio/ontheroad.mp3'));
    

    如果能正常播放,说明问题出在PlaylistProvider的路径赋值逻辑,需要排查是否有额外添加assets/前缀的代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 19:22:34