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

Flutter Web使用TensorFlow Lite音频分类器遇MissingPluginException

解决Flutter Web中Tflite音频分类器的MissingPluginException异常

异常原因

你遇到的MissingPluginException(No implementation found for method listen on channel FileRecognitionStream),本质是因为使用的tflite_audio插件未实现Web平台的对应通道方法——该插件通常仅支持Android和iOS,没有适配Web环境,导致Web端无法找到方法实现。

解决方案

方案1:改用TensorFlow.js适配Web端

Teachable Machine支持导出TensorFlow.js格式的模型,更适合Flutter Web场景,步骤如下:

  1. 导出TFJS模型
    打开你的Teachable Machine项目,点击「Export Model」→「TensorFlow.js」,下载包含model.json和权重文件的模型包,将其放到Flutter Web项目的web/assets/目录下。

  2. 添加依赖
    在pubspec.yaml中添加TFJS支持依赖:

    dependencies:
      flutter_tfjs: ^0.5.0
    
  3. 替换分类逻辑代码
    移除原tflite_audio相关代码,改用TFJS实现音频分类:

    import 'package:flutter_tfjs/flutter_tfjs.dart';
    import 'dart:html' as html;
    
    Future<void> runAudioClassification(String audioPath) async {
      // 加载TFJS模型
      final model = await tf.loadGraphModel('assets/model.json');
      
      // 读取并处理音频文件
      final audioBlob = html.Blob([html.HttpRequest.responseTypeBlob]);
      final audioFile = html.File([audioBlob], audioPath.split('/').last);
      final arrayBuffer = await audioFile.arrayBuffer();
      final audioContext = html.AudioContext();
      final audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
      final audioData = audioBuffer.getChannelData(0); // 获取单声道音频数据
      
      // 转换为模型所需的张量格式(需匹配Teachable Machine模型输入要求)
      final inputTensor = tf.tensor2d(audioData, [1, audioData.length]);
      
      // 执行预测
      final predictions = await model.predict(inputTensor);
      final resultData = await predictions.data;
      final inferenceTime = predictions.tensorStats().executionTimeMs;
      
      // 更新UI状态
      setState(() {
        recognition = resultData.toString();
        inferenceTime = '$inferenceTime ms';
      });
      
      // 释放张量资源,避免内存泄漏
      inputTensor.dispose();
      predictions.dispose();
      model.dispose();
    }
    

    修改你的stopRecording方法,调用新的分类逻辑:

    Future<void> stopRecording() async {
      try {
        String? path = await recorder.stop();
        setState(() {
          isListening = false;
          recordingPath = path!;
        });
        await runAudioClassification(recordingPath);
      } catch (e) {
        print('Error: $e');
      }
    }
    

方案2:排查插件Web支持(仅当插件声称支持Web时)

如果坚持使用tflite_audio,先做以下检查:

  • 查看插件的pub.dev页面,确认是否标记支持Web平台,若未标记则该插件不支持Web,需放弃或等待作者更新。
  • 检查web/index.html是否正确引入插件所需的JS脚本(部分Flutter Web插件需手动添加依赖)。
  • 执行flutter clean和flutter pub get,重新构建项目确保Web端代码被正确编译。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 19:22:46