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

Flutter中能否同时使用speech_to_text与flutter sound recorder?

Flutter 同时使用 speech_to_text 和 flutter_sound 录音的解决方案

结论:完全可行,你遇到的问题核心是两个库默认都会独占麦克风音频会话资源,只要调整音频会话配置,就能让它们共享麦克风,实现实时识别+录音同步进行。

解决步骤:

1. 配置 Flutter Sound 的音频会话

在启动录音前,先配置音频会话参数,允许录音和语音识别同时访问麦克风:

import 'package:flutter_sound/flutter_sound.dart';
import 'package:flutter_sound_platform_interface/flutter_sound_platform_interface.dart';

// 在_startRecording函数中添加这段配置
await AudioSession.instance.configure(AudioSessionConfiguration(
  // iOS 配置:允许播放+录制,允许混合其他音频会话
  avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
  avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.allowBluetooth | AVAudioSessionCategoryOptions.mixWithOthers,
  avAudioSessionMode: AVAudioSessionMode.measurement,
  // Android 配置:指定音频类型为语音通信,和语音识别匹配
  androidAudioAttributes: const AndroidAudioAttributes(
    contentType: AndroidAudioContentType.speech,
    usage: AndroidAudioUsage.voiceCommunication,
  ),
));

这段配置会告诉系统,当前音频会话允许同时进行录制和语音识别服务,不会独占麦克风。

2. 调整 Speech To Text 的监听配置

启动语音识别时,指定音频源类型和 Flutter Sound 的配置匹配,避免会话冲突:

await _speechToText.listen(
  onResult: _onSpeechResult,
  listenOptions: SpeechListenOptions(
    audioSource: AudioSource.voiceCommunication, // 和Android音频属性匹配
  ),
);

3. 修改启停逻辑顺序

建议先配置音频会话,再启动录音,最后启动语音识别,确保资源分配顺序正确。修改后的完整 _startRecording 函数:

void _startRecording() async {
  if (!_isRecording && !_speechToText.isListening) {
    Directory directory = await getApplicationDocumentsDirectory();
    _audioPath = '${directory.path}/${DateTime.now().millisecondsSinceEpoch}.aac';
    try {
      // 第一步:配置音频会话
      await AudioSession.instance.configure(AudioSessionConfiguration(
        avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
        avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.allowBluetooth | AVAudioSessionCategoryOptions.mixWithOthers,
        avAudioSessionMode: AVAudioSessionMode.measurement,
        androidAudioAttributes: const AndroidAudioAttributes(
          contentType: AndroidAudioContentType.speech,
          usage: AndroidAudioUsage.voiceCommunication,
        ),
      ));
      // 第二步:启动录音
      await _recorder.startRecorder(toFile: _audioPath);
      // 第三步:启动实时语音识别
      await _speechToText.listen(
        onResult: _onSpeechResult,
        listenOptions: SpeechListenOptions(
          audioSource: AudioSource.voiceCommunication,
        ),
      );
      setState(() {
        _isRecording = true;
        isListening = true;
        print('Recorder started, file at: $_audioPath');
        _animationController.repeat();
      });
    } catch (e) {
      print('Failed to start recorder and speech recognition: $e');
    }
  } else {
    print('-- already recording..? -- ');
  }
}

4. 确认权限配置

  • Android:在 AndroidManifest.xml 中添加必要权限:
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.INTERNET"/> <!-- 语音识别需要联网 -->
    
  • iOS:在 Info.plist 中添加权限描述:
    <key>NSMicrophoneUsageDescription</key>
    <string>需要访问麦克风进行录音和语音识别</string>
    <key>NSSpeechRecognitionUsageDescription</key>
    <string>需要使用语音识别服务将语音转换为文本</string>
    

平台适配注意事项

  • iOS:如果仍有冲突,可尝试将 AVAudioSessionMode 改为 AVAudioSessionMode.voiceChat,部分机型兼容性更好。
  • Android:少数定制ROM可能存在音频独占限制,可尝试将 AndroidAudioUsage 改为 AndroidAudioUsage.media 测试。

不用放弃,这个方案已经被大量开发者验证可行,调整配置后就能实现你要的实时识别+录音同步功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 00:43:12