ios_flutter_ai_plugin 0.0.1 ios_flutter_ai_plugin: ^0.0.1 copied to clipboard
腾讯云语音测评iOS端flutter插件
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:ios_flutter_ai_plugin/ios_flutter_ai_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _exampleText = '你好';
String _recordResult = 'Unknown';
var _controller = new TextEditingController();///朗读示例输入框
var _resultController = new TextEditingController();///测评结果输入框
@override
void initState() {
super.initState();
initPlatformState();
initPlugin();
initStream();
_controller.text = _exampleText;
}
//初始化initPlugin
Future<void> initPlugin() async {
//自行填入参数(发布正式环境记得更改参数)
TLAiConfig config = TLAiConfig(
appId: '',
secretId: '',
secretKey: '',
);
await IosFlutterAiPlugin.initPlugin(config);
}
void initStream() {
//1.测评结果;2.开始录制;3.结束录制;4.音量大小【0-120】;5.音频本地路径
IosFlutterAiPlugin.recordResultChannel.receiveBroadcastStream([1,2,3,4,5]).listen((event) {
//返回event皆为map
Map data = event;
data.forEach((key, value) {
if (key == 'recordResultKey') {
_recordResult = data[key];
print('_recordResult:$_recordResult');
_resultController.text = '';
_resultController.text = _recordResult;
setState(() {});
} else if (key == 'startRecordErrCodeKey') {
//0表示成功
int startRecordErrCode = data[key];
print('startRecordErrCode:$startRecordErrCode');
} else if (key == 'stopRecordErrCodeKey') {
//0表示成功
int stopRecordErrCode = data[key];
print('stopRecordErrCode:$stopRecordErrCode');
} else if (key == 'volumeKey') {
int volume = data[key];
print('volume:$volume');
} else if (key == 'audioPathKey') {
String audioPath = data[key];
print('audioPath:$audioPath');
}
});
});
}
// 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 IosFlutterAiPlugin.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;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Container (
padding: EdgeInsets.only(left: 0,top: 20,right: 0,bottom: 0),
child: Wrap(
spacing: 15,
runSpacing: 5,
children: [
TextField(
controller: _controller,
textAlign: TextAlign.center,
maxLines: 1,
keyboardType: TextInputType.multiline,
decoration: InputDecoration(
hintText: "请输入测评内容",
),
onSubmitted: (string) {
_exampleText = string;
// print("onSubmitted $string");
},
),
RaisedButton(
child: Text("音频录制"),
textColor: Colors.white,
color: Colors.blue,
onPressed: () async {
IosFlutterAiPlugin.startRecord(_exampleText, 1);
},
),
Text("测评结果",
textAlign: TextAlign.left,
style: TextStyle(fontWeight: FontWeight.normal),
),
TextField(
controller: _resultController,
textAlign: TextAlign.center,
maxLines: 20,
keyboardType: TextInputType.multiline,
),
],
),
),
),
);
}
}