xfvoicetest1 0.0.1
xfvoicetest1: ^0.0.1 copied to clipboard
xfvoice function test project by MegatechYan.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:xfvoicetest1/xfvoicetest1.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String voiceMsg = '暂无数据';
String iflyResultString = '按下方块说话';
late XFJsonResult xfResult;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// String platformVersion;
// // Platform messages may fail, so we use a try/catch PlatformException.
// // We also handle the message potentially returning null.
// try {
// platformVersion =
// await Test1.platformVersion ?? 'Unknown platform version';
// } on PlatformException {
// platformVersion = 'Failed to get platform version.';
// }
// // If the widget was removed from the tree while the asynchronous platform
// // message was in flight, we want to discard the reply rather than calling
// // setState to update our non-existent appearance.
// if (!mounted) return;
// setState(() {
// _platformVersion = platformVersion;
// });
final voice = Xfvoicetest1.shared;
// 请替换成你的appid
voice.init(appIdIos: '5df89707', appIdAndroid: '5df89707');
final param = new XFVoiceParam();
param.domain = 'iat';
// param.asr_ptt = '0'; //取消注释可去掉标点符号
param.asr_audio_path = 'audio.pcm';
param.result_type = 'json'; //可以设置plain
final map = param.toMap();
map['dwa'] = 'wpgs'; //设置动态修正,开启动态修正要使用json类型的返回格式
voice.setParameter(map);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '测试的demo',
home: Scaffold(
appBar: AppBar(
title: new Text('测试demo'),
),
body: Center(
child: GestureDetector(
child: Container(
child: Text(iflyResultString),
width: 300.0,
height: 300.0,
color: Colors.blueAccent,
),
onTapDown: (d) {
setState(() {
voiceMsg = '按下';
});
_recongize();
},
onTapUp: (d) {
_recongizeOver();
},
),
)
),
);
}
void _recongize() {
final listen = XFVoiceListener(
onVolumeChanged: (volume) {
},
onBeginOfSpeech: () {
xfResult = XFJsonResult('');
},
onResults: (String result, isLast) {
print('******************* ${xfResult.resultText()} ***********************');
if (xfResult == XFJsonResult('')) {
xfResult = XFJsonResult(result);
} else {
final another = XFJsonResult(result);
xfResult.mix(another);
}
if (result.length > 0) {
setState(() {
iflyResultString = xfResult.resultText();
});
}
},
onCompleted: (Map<dynamic, dynamic> errInfo, String filePath) {
setState(() {
iflyResultString = xfResult.resultText();
print('******************* ${xfResult.resultText()} ***********************');
});
}
);
Xfvoicetest1.shared.start(listener: listen);
}
void _recongizeOver() {
Xfvoicetest1.shared.stop();
}
}