Files
speech_text_ifly/lib/iflytek_speech_demo.dart

65 lines
1.6 KiB
Dart
Raw 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> start(EventHandler onReceiveSpeechText) async {
_onReceiveSpeechText = onReceiveSpeechText;
try{
return await _channel.invokeMethod('start');
}catch(e) {
print(e);
return "9999999";
}
}
Future<String> stop() async {
return await _channel.invokeMethod('stop');
}
Future<String> cancel() async {
return await _channel.invokeMethod('cancel');
}
}