Files
speech_text_ifly/lib/iflytek_speech_demo.dart

88 lines
2.1 KiB
Dart
Raw Permalink Normal View History

2022-01-24 16:10:41 +08:00
import 'dart:async';
import 'package:flutter/services.dart';
typedef Future<dynamic> EventHandler(Map<String, dynamic> event);
class IflytekSpeechDemo {
static const MethodChannel _channel = MethodChannel('iflytek_speech_demo');
late EventHandler _onReceiveSpeechText;
// test方法
static Future<String?> get platformVersion async {
final String? version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
// 编写自定义异步方法调用原生sdk传递过来的数据
// 1. 初始化方法
Future<int> init(String appId) async {
try{
int ret = await _channel.invokeMethod('init', {
"appID": appId
});
if(ret == 0) {
_channel.setMethodCallHandler(_handleMethod);
}
return ret;
}catch(e) {
print(e);
return 9999999;
}
}
Future<Future> _handleMethod(MethodCall call) async {
print("speech_handleMethod:");
switch (call.method) {
case "onReceiveSpeechText":
return _onReceiveSpeechText(call.arguments.cast<String, dynamic>());
default:
throw UnsupportedError("Unrecognized Event:"+call.method);
}
}
Future<String> wakeupStart(EventHandler onReceiveSpeechText) async {
2022-01-24 16:10:41 +08:00
_onReceiveSpeechText = onReceiveSpeechText;
try{
return await _channel.invokeMethod('wakeupStart');
2022-01-24 16:10:41 +08:00
}catch(e) {
print(e);
return "9999999";
}
}
Future<String> wakeupStop() async {
return await _channel.invokeMethod('wakeupStop');
2022-01-24 16:10:41 +08:00
}
Future<String> wakeupCancel() async {
return await _channel.invokeMethod('wakeupCancel');
}
Future<int> ttsStart(String ttsText) async {
try{
int ret = await _channel.invokeMethod('ttsStart', {
"ttsText": ttsText
});
return ret;
}catch(e) {
print(e);
return 9999999;
}
}
Future<String> ttsCancel() async {
return await _channel.invokeMethod('ttsCancel');
}
Future<String> ttsPause() async {
return await _channel.invokeMethod('ttsPause');
}
Future<String> ttsResume() async {
return await _channel.invokeMethod('ttsResume');
2022-01-24 16:10:41 +08:00
}
}