import 'dart:async'; import 'package:flutter/services.dart'; /// 讯飞 SparkChain。事件从 [events] 来,方法调用只返回启动码。 class SparkChain { static const _method = MethodChannel('sparkchain'); static const _events = EventChannel('sparkchain/events'); static Stream>? _stream; static Stream> get events { _stream ??= _events.receiveBroadcastStream().map((event) { final raw = Map.from(event as Map); return raw.map((key, value) => MapEntry(key.toString(), value)); }); return _stream!; } /// 0 成功。不打印密钥。 static Future init({ required String appId, required String apiKey, required String apiSecret, int apiType = 1, }) async { final code = await _method.invokeMethod('init', { 'appId': appId, 'apiKey': apiKey, 'apiSecret': apiSecret, 'apiType': apiType, }); return code ?? -1; } static Future translate({ required String id, required String from, required String to, required String text, }) async { final code = await _method.invokeMethod('translate', { 'id': id, 'from': from, 'to': to, 'text': text, }); return code ?? -1; } static Future speak({ required String id, required String lang, required String text, String? voice, }) async { final code = await _method.invokeMethod('speak', { 'id': id, 'lang': lang, 'text': text, if (voice != null) 'voice': voice, }); return code ?? -1; } /// [lang] 用短码 zh / en / ga。ga 走实时转写,其余走听写。 static Future startListen({ required String id, required String lang, }) async { final code = await _method.invokeMethod('startListen', { 'id': id, 'lang': lang, }); return code ?? -1; } static Future write(Uint8List pcm) async { final code = await _method.invokeMethod('write', {'pcm': pcm}); return code ?? -1; } static Future stopListen() async { await _method.invokeMethod('stopListen'); } } /// 把界面上的 BCP-47 收成插件认的短码。不认识的原样返回,让原生按失败处理。 String sparkChainShortCode(String code) { final raw = code.trim().toLowerCase().replaceAll('_', '-'); if (raw.isEmpty) return raw; if (raw == 'ga' || raw.startsWith('ga-')) return 'ga'; if (raw == 'zh' || raw.startsWith('zh-')) return 'zh'; if (raw == 'en' || raw.startsWith('en-')) return 'en'; final dash = raw.indexOf('-'); return dash < 0 ? raw : raw.substring(0, dash); }