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