You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.0 KiB

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../data/services/voice_recognition_service.dart';
class SpeechDemoPage extends StatefulWidget {
const SpeechDemoPage({Key? key}) : super(key: key);
@override
_SpeechDemoPageState createState() => _SpeechDemoPageState();
}
class _SpeechDemoPageState extends State<SpeechDemoPage> {
final VoiceRecognitionService _recognitionService = Get.find<VoiceRecognitionService>();
@override
void initState() {
super.initState();
_initializeEngine();
}
Future<void> _initializeEngine() async {
try {
await _recognitionService.initialize();
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('初始化失败: $e')),
);
}
}
}
Future<void> _startRecognition() async {
try {
await _recognitionService.startRecognition();
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('启动识别失败: $e')),
);
}
}
}
Future<void> _stopRecognition() async {
try {
await _recognitionService.stopRecognition();
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('停止识别失败: $e')),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('火山语音识别演示'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 状态显示
Obx(() {
String status = '准备中';
if (_recognitionService.isConnecting.value) {
status = '连接中...';
} else if (_recognitionService.isInitialized.value) {
status = _recognitionService.isListening.value ? '正在听...' : '就绪';
}
return Text('状态: $status', style: const TextStyle(fontSize: 18));
}),
const SizedBox(height: 20),
// 识别结果显示
Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
width: double.infinity,
height: 200,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
padding: const EdgeInsets.all(16),
child: Obx(() {
return SingleChildScrollView(
child: Text(
_recognitionService.recognizedText.value,
style: const TextStyle(fontSize: 16),
),
);
}),
),
),
const SizedBox(height: 20),
// 控制按钮
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Obx(() {
return ElevatedButton(
onPressed: _recognitionService.isInitialized.value && !_recognitionService.isListening.value
? _startRecognition
: null,
child: const Text('开始识别'),
);
}),
const SizedBox(width: 20),
Obx(() {
return ElevatedButton(
onPressed: _recognitionService.isListening.value
? _stopRecognition
: null,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
),
child: const Text('停止识别'),
);
}),
],
),
],
),
),
);
}
}