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

Flutter just_audio报错:未找到disposeAllPlayers等方法实现求解决

问题描述

在API 30的Android设备上,使用just_audio包播放从服务器获取的WAV格式base64字符串音频时,出现以下错误:

E/flutter (31770): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method disposeAllPlayers on channel com.ryanheise.just_audio.methods)
E/flutter (31770): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #1      MethodChannelJustAudio.disposeAllPlayers (package:just_audio_platform_interface/method_channel_just_audio.dart:29:10)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): 
E/flutter (31770): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method init on channel com.ryanheise.just_audio.methods)
E/flutter (31770): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #1      MethodChannelJustAudio.init (package:just_audio_platform_interface/method_channel_just_audio.dart:13:5)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #2      AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1330:13)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): 

自定义StreamAudioSource实现:

class MyAudioPlayer extends StreamAudioSource {
  MyAudioPlayer({this.bytes});

  List<int> bytes;

  @override
  Future<StreamAudioResponse> request([int start, int end]) async {
    final start = 0;
    final end = bytes.length;
    return StreamAudioResponse(
      sourceLength: bytes.length,
      contentLength: end - start,
      offset: start,
      stream: Stream.value(bytes.sublist(start, end)),
      contentType: "audio/x-wav",
    );
  }
}

播放逻辑:

void playTTS(String content) async{
  Dialogs.showLoadingDialog(context: context);
  final List<int> base64 = await ApiService.textToSpeech(content);
  final player = AudioPlayer();
  await player.setAudioSource(MyAudioPlayer(bytes: base64));
  await player.play();
  Navigator.of(context, rootNavigator: true).pop();
  listString.value = List.from(listString.value..add(ValueNotifier(content)));
}

已在AndroidManifest.xml中设置android:usesCleartextTraffic="true"。

解决方案
  • 重新构建插件绑定
    该错误多因Flutter插件与原生端未正确绑定导致,执行以下步骤修复:

    1. 停止当前运行的Flutter应用
    2. 清理缓存:flutter clean
    3. 重新获取依赖:flutter pub get
    4. 删除android/.gradle目录后,重新执行flutter run
  • 检查版本兼容性
    确认pubspec.yaml中just_audio版本与当前Flutter SDK兼容,建议使用最新稳定版。执行flutter pub outdated查看依赖更新情况,更新后重复上述构建步骤。

  • 规范播放器生命周期管理
    当前代码每次播放创建新AudioPlayer实例但未释放资源,易引发通道冲突。修改播放逻辑,添加资源释放:

    void playTTS(String content) async{
      Dialogs.showLoadingDialog(context: context);
      final List<int> base64 = await ApiService.textToSpeech(content);
      final player = AudioPlayer();
      try {
        await player.setAudioSource(MyAudioPlayer(bytes: base64));
        await player.play();
        // 播放结束后自动释放资源
        player.playerStateStream.listen((state) {
          if (state.processingState == ProcessingState.completed) {
            player.dispose();
          }
        });
      } catch(e) {
        print("播放错误: $e");
        player.dispose();
      } finally {
        Navigator.of(context, rootNavigator: true).pop();
        listString.value = List.from(listString.value..add(ValueNotifier(content)));
      }
    }
    
  • 检查Android配置
    确保android/app/build.gradle中minSdkVersion不低于just_audio要求的版本(当前稳定版要求≥21),同时确认compileSdkVersion和targetSdkVersion适配API 30。

  • 避免热重载引发的通道异常
    插件通道初始化可能在热重载时失效,遇到该错误时需完全停止应用后重新启动,不要依赖热重载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:40:50