88 lines
2.1 KiB
Dart
88 lines
2.1 KiB
Dart
|
||
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 {
|
||
_onReceiveSpeechText = onReceiveSpeechText;
|
||
try{
|
||
return await _channel.invokeMethod('wakeupStart');
|
||
}catch(e) {
|
||
print(e);
|
||
return "9999999";
|
||
}
|
||
}
|
||
|
||
Future<String> wakeupStop() async {
|
||
return await _channel.invokeMethod('wakeupStop');
|
||
}
|
||
|
||
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');
|
||
}
|
||
|
||
}
|