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

寻求Flutter平台背景声音转文本插件的技术方案推荐

Flutter 背景声音转文本插件推荐与实现方案

核心插件推荐

  • speech_to_text:社区使用率最高的实时语音转写插件,支持多语言识别,适配对话、旁白等多种音频场景,配置简单,直接调用系统原生语音识别服务。
  • flutter_speech_recognition:轻量级实现,依赖iOS SiriKit、Android Google语音识别等系统原生能力,集成成本低,适合快速搭建语音转文本功能。
  • vosk_flutter:主打离线语音识别,无需联网即可完成转写,支持自定义训练模型,适合对数据隐私要求高或无网络场景下的连续对话转录。

快速实现示例(以speech_to_text为例)

  1. 添加依赖到pubspec.yaml:
dependencies:
  speech_to_text: ^6.5.0
  1. 基础功能实现代码:
import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;

class SpeechToTextPage extends StatefulWidget {
  const SpeechToTextPage({super.key});

  @override
  State<SpeechToTextPage> createState() => _SpeechToTextPageState();
}

class _SpeechToTextPageState extends State<SpeechToTextPage> {
  final stt.SpeechToText _speech = stt.SpeechToText();
  bool _isListening = false;
  String _transcribedText = '';

  @override
  void initState() {
    super.initState();
    _initSpeechRecognition();
  }

  // 初始化语音识别服务
  void _initSpeechRecognition() async {
    bool isAvailable = await _speech.initialize(
      onStatus: (status) => debugPrint('识别状态:$status'),
      onError: (error) => debugPrint('识别错误:${error.errorMsg}'),
    );
    if (isAvailable) {
      debugPrint('语音识别服务已就绪');
    }
  }

  // 启动/停止语音监听
  void _toggleListening() async {
    if (!_isListening) {
      bool started = await _speech.listen(
        onResult: (result) {
          setState(() {
            _transcribedText = result.recognizedWords;
          });
        },
        listenMode: stt.ListenMode.dictation, // 适配对话类场景
        localeId: 'zh_CN', // 指定中文识别
        cancelOnError: true,
      );
      setState(() => _isListening = started);
    } else {
      _speech.stop();
      setState(() => _isListening = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('语音转文本')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            Expanded(
              child: Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Text(_transcribedText.isEmpty ? '等待语音输入...' : _transcribedText),
                ),
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _toggleListening,
              child: Text(_isListening ? '停止识别' : '开始识别'),
            ),
          ],
        ),
      ),
    );
  }
}

关键注意事项

  • 权限配置:
    • Android:在AndroidManifest.xml中添加 <uses-permission android:name="android.permission.RECORD_AUDIO" />
    • iOS:在Info.plist中添加 NSSpeechRecognitionUsageDescription(语音识别权限说明)和 NSMicrophoneUsageDescription(麦克风权限说明)
  • 离线/在线选择:在线识别(speech_to_text、flutter_speech_recognition)准确率更高,但依赖网络;离线识别(vosk_flutter)需要下载对应语言的模型文件,占用本地存储但无需网络。
  • 后台识别:如果需要在App后台持续转录声音,Android需配置后台服务,iOS需开启音频后台模式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 02:52:21