flutter如何实现语音文件转文字

在Flutter中如何将本地存储的语音文件转换为文字?目前项目需要实现这个功能,但不太清楚具体该用什么方案。是否有推荐的第三方插件或API可以实现这个功能?最好能支持常见音频格式如MP3、WAV,并且转换准确率较高。另外,如果涉及到收费服务,希望能说明大致的费用情况。求有经验的大佬分享具体实现步骤或代码示例!

2 回复

使用Flutter实现语音转文字,可借助第三方服务如Google Speech-to-Text API或Azure Speech Services。步骤如下:

  1. 录音:使用flutter_soundaudioplayers录制音频。
  2. 上传:将音频文件发送至语音识别API。
  3. 转换:API返回识别后的文本结果。

示例代码可使用speech_to_text插件简化流程。

更多关于flutter如何实现语音文件转文字的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中实现语音文件转文字,可以通过以下步骤实现:

1. 选择语音识别方案

  • 云端方案:使用Google Cloud Speech-to-Text、Azure Speech Services或阿里云语音识别等,适合长音频或高精度需求。
  • 本地方案:使用speech_to_textflutter_tts等插件,适合短语音或离线场景。

2. 实现步骤(以Google Cloud Speech-to-Text为例)

依赖配置

pubspec.yaml中添加HTTP依赖:

dependencies:
  http: ^0.13.0

代码实现

import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';

class SpeechToTextService {
  static const String _apiUrl = 'https://speech.googleapis.com/v1/speech:recognize';
  final String _apiKey; // 从Google Cloud控制台获取

  SpeechToTextService(this._apiKey);

  Future<String> transcribeAudio(String filePath) async {
    // 读取音频文件
    File audioFile = File(filePath);
    List<int> audioBytes = await audioFile.readAsBytes();

    // 构建请求体
    Map<String, dynamic> requestBody = {
      'config': {
        'encoding': 'LINEAR16', // 根据音频格式调整
        'sampleRateHertz': 16000,
        'languageCode': 'zh-CN', // 中文普通话
      },
      'audio': {
        'content': base64.encode(audioBytes),
      },
    };

    // 发送请求
    final response = await http.post(
      Uri.parse('$_apiUrl?key=$_apiKey'),
      headers: {'Content-Type': 'application/json'},
      body: json.encode(requestBody),
    );

    // 解析响应
    if (response.statusCode == 200) {
      var result = json.decode(response.body);
      return result['results'][0]['alternatives'][0]['transcript'] ?? '';
    } else {
      throw Exception('识别失败: ${response.body}');
    }
  }
}

3. 使用示例

// 在Flutter中调用
String result = await SpeechToTextService('YOUR_API_KEY')
    .transcribeAudio('/path/to/audio.wav');
print('识别结果: $result');

4. 注意事项

  • 音频格式:支持WAV、FLAC等格式,需正确配置encodingsampleRateHertz
  • 权限处理:需要文件读取权限,在android/app/src/main/AndroidManifest.xml中添加:
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
  • 错误处理:添加网络异常和API限额处理。

替代方案

  • 本地识别:使用speech_to_text插件(仅支持短语音):
    import 'package:speech_to_text/speech_to_text.dart' as stt;
    
    final stt.SpeechToText speech = stt.SpeechToText();
    bool available = await speech.initialize();
    if (available) {
      speech.listen(onResult: (result) => print(result.recognizedWords));
    }
    

选择方案时需根据需求平衡精度、成本和离线能力。

回到顶部