Flutter语音识别插件frame_transcribe_googlespeech的使用
Flutter语音识别插件frame_transcribe_googlespeech的使用
Frame Transcribe - Google Cloud Speech API(在线语音转文字、实时字幕、Frame麦克风)
连接到Brilliant Labs Frame,从Frame麦克风流式传输音频,并通过Google的基于云的流式转录服务将其发送,实时在Frame显示屏上显示流式文本。
Google的基于云的语音转文字流式API支持多种语言,并且此应用目前使用telephony模型类型。
然而,Frame显示屏仅以拉丁文脚本显示纯文本,因此当前此演示应用程序仅对部分语言有用。
Google Cloud API设置
注意:截至撰写时,Google提供每月免费60分钟的语音转文字服务,但必须启用账单并启用Speech-to-Text API。Google似乎保留了将未计费转录用于其训练数据的权利,但不会对付费API使用这样做,因此始终考虑您传输的内容。
- 按照指南设置自己的Google Cloud项目并启用Speech-to-Text API(使用v2)。
- 创建一个“服务账户”进行授权,并保存JSON字符串以输入到应用程序对应的文本框中(只需一次——它会被持久化)。
- 在应用程序的相应文本框中输入您的项目ID(例如
frame-transcribe),也将会被持久化。 - 输入来自支持的语言列表的语言代码(目前仅支持
telephony模型),例如en-US或de-DE,记住Frame显示屏仅限于拉丁文脚本的纯文本。
示例截图
示例代码
以下是一个完整的Flutter示例代码,展示如何使用frame_transcribe_googlespeech插件实现语音识别功能:
import 'package:flutter/material.dart';
import 'package:frame_transcribe_googlespeech/frame_transcribe_googlespeech.dart'; // 导入插件
import 'package:googleapis_auth/auth_io.dart'; // 用于Google API认证
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: VoiceRecognitionPage(),
);
}
}
class VoiceRecognitionPage extends StatefulWidget {
[@override](/user/override)
_VoiceRecognitionPageState createState() => _VoiceRecognitionPageState();
}
class _VoiceRecognitionPageState extends State<VoiceRecognitionPage> {
String _recognizedText = ''; // 用于存储识别结果
bool _isListening = false; // 控制是否正在监听
// 初始化插件
final _speechToText = FrameTranscribeGooglespeech();
[@override](/user/override)
void initState() {
super.initState();
// 加载Google API认证信息
_speechToText.initialize(
projectId: 'your-project-id', // 替换为您的Google Cloud项目ID
credentials:
'{"type":"service_account","project_id":"your-project-id","private_key_id":"your-private-key-id","private_key":"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\\n","client_email":"your-service-account-email","client_id":"your-client-id","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/your-service-account-email"}',
languageCode: 'en-US', // 替换为您要使用的语言代码
);
}
// 开始监听语音
void _startListening() async {
setState(() {
_isListening = true;
});
// 开始流式语音识别
await _speechToText.startStreamingRecognition().listen((result) {
setState(() {
_recognizedText = result.text; // 更新识别结果
});
}).onDone(() {
setState(() {
_isListening = false;
});
});
}
// 停止监听语音
void _stopListening() async {
setState(() {
_isListening = false;
});
await _speechToText.stopStreamingRecognition();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('语音识别示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'识别结果:',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
Text(
_recognizedText,
style: TextStyle(fontSize: 18, color: Colors.black),
),
SizedBox(height: 40),
ElevatedButton(
onPressed: _isListening ? _stopListening : _startListening,
child: Text(_isListening ? '停止监听' : '开始监听'),
),
],
),
),
);
}
}
更多关于Flutter语音识别插件frame_transcribe_googlespeech的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter语音识别插件frame_transcribe_googlespeech的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
frame_transcribe_googlespeech 是一个 Flutter 插件,用于在 Flutter 应用中实现语音识别功能。它基于 Google Speech-to-Text API,可以将实时语音转换为文本。以下是使用该插件的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml 文件中添加 frame_transcribe_googlespeech 插件的依赖:
dependencies:
flutter:
sdk: flutter
frame_transcribe_googlespeech: ^<latest_version>
然后运行 flutter pub get 以安装依赖。
2. 配置 Google Speech-to-Text API
为了使用 Google Speech-to-Text API,你需要在 Google Cloud Platform (GCP) 上启用 Speech-to-Text API 并获取 API 密钥。
- 访问 Google Cloud Console.
- 创建一个新的项目或选择现有项目。
- 在左侧导航栏中,选择 “API & Services” > “Library”。
- 搜索 “Speech-to-Text API” 并启用它。
- 在 “API & Services” > “Credentials” 中,创建一个 API 密钥。
3. 初始化插件
在你的 Flutter 应用中初始化 frame_transcribe_googlespeech 插件,并设置 API 密钥。
import 'package:frame_transcribe_googlespeech/frame_transcribe_googlespeech.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 设置 Google Speech-to-Text API 密钥
await FrameTranscribeGooglespeech.initialize(apiKey: 'YOUR_API_KEY');
runApp(MyApp());
}
4. 开始语音识别
使用 FrameTranscribeGooglespeech 类的 startListening 方法开始语音识别。你可以设置语言代码和其他参数。
void startSpeechRecognition() async {
try {
await FrameTranscribeGooglespeech.startListening(
languageCode: 'en-US', // 设置语言代码
onResult: (String result) {
print('识别结果: $result');
},
onError: (String error) {
print('识别错误: $error');
},
);
} catch (e) {
print('启动语音识别失败: $e');
}
}
5. 停止语音识别
使用 stopListening 方法停止语音识别。
void stopSpeechRecognition() async {
await FrameTranscribeGooglespeech.stopListening();
}
6. 处理识别结果
在 onResult 回调中处理识别结果,并在 UI 中显示。
String transcription = '';
void startSpeechRecognition() async {
try {
await FrameTranscribeGooglespeech.startListening(
languageCode: 'en-US',
onResult: (String result) {
setState(() {
transcription = result;
});
},
onError: (String error) {
setState(() {
transcription = 'Error: $error';
});
},
);
} catch (e) {
setState(() {
transcription = 'Failed to start: $e';
});
}
}
7. 在 UI 中使用
在 Flutter 应用的 UI 中添加按钮来启动和停止语音识别,并显示识别结果。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('语音识别示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: startSpeechRecognition,
child: Text('开始识别'),
),
ElevatedButton(
onPressed: stopSpeechRecognition,
child: Text('停止识别'),
),
SizedBox(height: 20),
Text('识别结果: $transcription'),
],
),
),
),
);
}
}
8. 处理权限
确保在 AndroidManifest.xml 和 Info.plist 中添加必要的权限,以允许应用访问麦克风。
Android:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
iOS:
<key>NSMicrophoneUsageDescription</key>
<string>我们需要访问您的麦克风以进行语音识别。</string>

