xfmsc 0.0.4
xfmsc: ^0.0.4 copied to clipboard
科大讯飞MSC(Mobile Speech Client)flutter插件, 目前支持语音评测
example/lib/main.dart
// ignore_for_file: avoid_print
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:xfmsc/ise/evaluator_listener_interface.dart';
import 'package:xfmsc/ise/evaluator_result.dart';
import 'package:xfmsc/ise/speech_evaluator_interface.dart';
import 'package:xfmsc/msc/msc_error.dart';
import 'package:xfmsc/xfmsc.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> implements EvaluatorListener {
bool _isEvaluating = false;
bool _sdkIsEvaluating = false;
String _result = '';
final FocusNode _textFocus = FocusNode();
final TextEditingController _textController = TextEditingController();
final speechEvaluator = Xfmsc.getSpeechEvaluator();
@override
void initState() {
super.initState();
initSpeechEvaluator();
}
@override
void dispose() {
super.dispose();
speechEvaluator.removeEvaluatingListener(this);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initSpeechEvaluator() async {
// 雅思
try {
// appId写死就行了, 跟sdk文件绑定的
await Xfmsc.init('530601f5');
await speechEvaluator.setParameters({
// 固定英文
SpeechEvaluatorParam.ent: 'en_vip',
// 句子, 根据需求选取合适的评测题型
SpeechEvaluatorParam.category: 'read_sentence',
// 固定utf-8
SpeechEvaluatorParam.textEncoding: 'utf-8',
// 关闭静音抑制
SpeechEvaluatorParam.vadEnable: '0',
// 音频文件保存为wav格式
SpeechEvaluatorParam.audioFormat: 'wav',
// 音频文件保存路径(临时文件)
SpeechEvaluatorParam.audioPath:
'${(await getTemporaryDirectory()).absolute.path}/speech/latest.wav',
// 百分制
SpeechEvaluatorParam.iseUnite: '1',
// 百分制
SpeechEvaluatorParam.rst: 'entirety',
// 百分制
SpeechEvaluatorParam.extraAbility:
'syll_phone_err_msg;pitch;multi_dimension',
});
speechEvaluator.addEvaluatingListener(this);
setState(() {
_result += 'sdk已初始化';
});
} catch (e) {
setState(() {
_result += '出错了$e\n';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: (Container(
margin: const EdgeInsets.all(30),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text('是否在录音: $_isEvaluating'),
Text('sdk是否在录音: $_sdkIsEvaluating'),
Container(
margin: const EdgeInsets.only(top: 10),
child: SizedBox(
height: 250,
child: SingleChildScrollView(
scrollDirection: Axis.vertical, child: Text(_result)))),
TextField(
controller: _textController,
focusNode: _textFocus,
onTapOutside: (_) {
_textFocus.unfocus();
},
decoration: const InputDecoration(
hintText: 'Apple pie.',
)),
MaterialButton(
color: Colors.green,
child: const Text('开始评测'),
onPressed: () async {
if (await Permission.microphone.request().isGranted) {
final startResult = await speechEvaluator.startEvaluating(
_textController.text.isEmpty
? 'Apple pie.'
: _textController.text,
null);
final isEvaluating = await speechEvaluator.isEvaluating();
if (startResult == 0) {
setState(() {
_isEvaluating = true;
_sdkIsEvaluating = isEvaluating;
_result += '开始评测\n';
});
} else {
setState(() {
_result += '开始评测失败: $startResult\n';
});
}
} else {
setState(() {
_result += '没有录音权限\n';
});
}
}),
MaterialButton(
color: Colors.red,
child: const Text('结束评测'),
onPressed: () async {
await speechEvaluator.stopEvaluating();
setState(() {
_result += '结束评测\n';
});
}),
MaterialButton(
color: Colors.orange,
child: const Text('取消'),
onPressed: () async {
await speechEvaluator.cancel();
final isEvaluating = await speechEvaluator.isEvaluating();
setState(() {
_isEvaluating = false;
_sdkIsEvaluating = isEvaluating;
_result += '取消评测\n';
});
})
]),
)),
),
);
}
@override
void onBeginOfSpeech() {
print('EvaluatorListener, onBeginOfSpeech');
setState(() {
_result += 'onBeginOfSpeech\n';
});
}
@override
void onEndOfSpeech() {
print('EvaluatorListener, onEndOfSpeech');
setState(() {
_result += 'onEndOfSpeech\n';
});
}
@override
void onError(MscError error) async {
print(
'EvaluatorListener, onError, ${error.errorCode}, ${error.errorDescription}');
final isEvaluating = await speechEvaluator.isEvaluating();
setState(() {
_isEvaluating = false;
_sdkIsEvaluating = isEvaluating;
_result += 'onError: ${error.errorDescription}\n';
});
}
@override
void onResult(EvaluatorResult? result, bool isLast) async {
print('EvaluatorListener, onResult, ${result?.resultString}, $isLast');
if (isLast) {
final isEvaluating = await speechEvaluator.isEvaluating();
setState(() {
_isEvaluating = false;
_sdkIsEvaluating = isEvaluating;
_result += result?.resultString ?? '评测结果为null?\n';
});
}
}
@override
void onVolumeChanged(int volume) {}
}