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

使用Flutter Just Audio插件播放音频时出现Source Error问题求助

问题

开发音乐播放器时,需要播放自定义播放列表文件夹内的音频,使用on_audio_query插件查询该文件夹中的音频,just_audio作为播放器,但播放歌曲时总是返回“Source error”错误。

相关代码:

FutureBuilder<List<SongModel>>(
          future: _audioQuery.queryAudiosFrom(
              AudiosFromType.PLAYLIST, widget.playlistpath,
              ignoreCase: true),
          builder: (context, item) {
            if (item.data == null) {
              return const Text("Searching for audio");
            }
            if (item.data!.isEmpty) {
              return const Text("No music Audio found");
            }
            return ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: item.data!.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(item.data![index].title),
                    onTap: () async {
                      try {
                        debugPrint("${item.data![index].uri}");
                        await _player.setAudioSource(AudioSource.uri(
                            Uri.parse(item.data![index].uri!)));
                        // await _player.setAudioSource(
                        //     createPlaylist(item.data!),
                        //     initialIndex: index);
                        await _player.play();
                      } catch (e) {
                        debugPrint("$e");
                      }
                    },
                  );
                });
          }),

报错信息:

I/flutter (18513): (0) Source error

可能的解决方案
  • 检查URI格式有效性:先确认debugPrint输出的URI是否为完整的file://开头格式。如果on_audio_query返回的uri缺少该前缀,直接解析会失败,需手动拼接:
    String uri = item.data![index].uri!;
    if (!uri.startsWith('file://')) {
      uri = 'file://$uri';
    }
    await _player.setAudioSource(AudioSource.uri(Uri.parse(uri)));
    
  • 验证文件访问权限:确保应用已获取对应存储权限——Android 13+需要READ_MEDIA_AUDIO,Android 12及以下需要READ_EXTERNAL_STORAGE。无权限时just_audio无法访问音频文件,可通过权限插件检查并请求权限。
  • 确认文件真实存在:通过URI对应的路径检查文件是否存在,避免因文件被删除或路径错误导致播放失败:
    File file = File(Uri.parse(item.data![index].uri!).path);
    if (!await file.exists()) {
      debugPrint("文件不存在:${file.path}");
      return;
    }
    
  • 检查音频格式兼容性:just_audio对部分小众格式支持有限,优先测试MP3、FLAC、AAC等常见格式的音频文件,排除格式不兼容问题。
  • 确认播放器实例初始化:保证_player已在initState中完成初始化(如_player = AudioPlayer();),且未被提前释放。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 19:15:34