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.
184 lines
5.6 KiB
184 lines
5.6 KiB
import 'dart:async';
|
|
import 'dart:developer' as developer;
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
|
|
import '../../data/services/voice_recognition_service.dart';
|
|
|
|
class SpeechDemoPage extends StatefulWidget {
|
|
const SpeechDemoPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<SpeechDemoPage> createState() => _SpeechDemoPageState();
|
|
}
|
|
|
|
class _SpeechDemoPageState extends State<SpeechDemoPage> {
|
|
late final VoiceRecognitionService _voiceService;
|
|
bool _isInitialized = false;
|
|
bool _isRecognizing = false;
|
|
String _recognizedText = '';
|
|
String _statusMessage = '准备就绪';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initializeService();
|
|
}
|
|
|
|
Future<void> _initializeService() async {
|
|
setState(() {
|
|
_statusMessage = '正在初始化语音服务...';
|
|
});
|
|
|
|
try {
|
|
// 创建服务实例(这一步会自动从环境变量加载配置)
|
|
_voiceService = VoiceRecognitionService();
|
|
|
|
// 初始化语音识别服务
|
|
final result = await _voiceService.initialize();
|
|
|
|
setState(() {
|
|
_isInitialized = result;
|
|
_statusMessage = '语音服务初始化成功,可以开始识别';
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_isInitialized = false;
|
|
_statusMessage = '初始化失败: $e';
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _startVoiceRecognition() async {
|
|
if (!_isInitialized) {
|
|
setState(() {
|
|
_statusMessage = '语音服务未初始化,请先初始化';
|
|
});
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isRecognizing = true;
|
|
_statusMessage = '正在聆听...';
|
|
});
|
|
|
|
try {
|
|
final result = await _voiceService.recognizeSpeech();
|
|
setState(() {
|
|
_recognizedText = result;
|
|
_isRecognizing = false;
|
|
_statusMessage = '识别完成';
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_isRecognizing = false;
|
|
_statusMessage = '识别失败: $e';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('语音识别演示'),
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
// 状态信息
|
|
Container(
|
|
padding: const EdgeInsets.all(12),
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey[200],
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
_statusMessage,
|
|
style: TextStyle(
|
|
color: _isInitialized ? Colors.green[700] : Colors.red[700],
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
// 识别结果显示区域
|
|
Expanded(
|
|
child: Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.grey[300]!),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: _recognizedText.isEmpty
|
|
? const Center(
|
|
child: Text(
|
|
'识别结果将显示在这里',
|
|
style: TextStyle(color: Colors.grey),
|
|
),
|
|
)
|
|
: SingleChildScrollView(
|
|
child: Text(
|
|
_recognizedText,
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
height: 1.5,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
// 操作按钮
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: [
|
|
ElevatedButton(
|
|
onPressed: _isRecognizing ? null : _initializeService,
|
|
style: ElevatedButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
|
),
|
|
child: const Text('重新初始化'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: _isRecognizing || !_isInitialized ? null : _startVoiceRecognition,
|
|
style: ElevatedButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
|
backgroundColor: Colors.blue[700],
|
|
),
|
|
child: Text(_isRecognizing ? '正在识别...' : '开始语音识别'),
|
|
),
|
|
],
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
// 提示信息
|
|
const Text(
|
|
'提示:请在安静的环境中使用,并确保已授予应用录音权限。',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontStyle: FontStyle.italic,
|
|
color: Colors.grey,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|