speech_plugin.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. class SpeechPlugin {
  4. static const MethodChannel _channel =
  5. const MethodChannel('speech_plugin');
  6. static final SpeechPlugin _instance = SpeechPlugin();
  7. StreamController _onEvaluatorChange;
  8. Stream<Map> get evaluator => _onEvaluatorChange.stream;
  9. static int evaluatorType = 0;
  10. static SpeechPlugin get instance => _instance;
  11. static Future<String> get platformVersion async {
  12. final String version = await _channel.invokeMethod('getPlatformVersion');
  13. return version;
  14. }
  15. Future<void> initSdk() {
  16. return _channel.invokeMethod("initSpeechSdk");
  17. }
  18. void updateEvaluatorType(int type) {
  19. SpeechPlugin.evaluatorType = type; // 1为开启全维度,0为关闭
  20. }
  21. Future<Map> evaluatorByAudio(String pathEvaluatorDecode, int index, String videoId, String recordPath, String en, {int evaluatorType}) {
  22. return _channel.invokeMethod("evaluatorByAudio", {
  23. "pathEvaluatorDecode": pathEvaluatorDecode,
  24. "index": index,
  25. "videoId": videoId,
  26. "recordPath": recordPath,
  27. "en": en,
  28. "evaluatorType": evaluatorType ?? SpeechPlugin.evaluatorType,
  29. });
  30. }
  31. Future<Map> evaluatorByMp4(String pathEvaluatorDecode, int index, String videoId, String recordPath, String en, {int evaluatorType}) {
  32. return _channel.invokeMethod("evaluatorByMp4", {
  33. "pathEvaluatorDecode": pathEvaluatorDecode,
  34. "index": index,
  35. "videoId": videoId,
  36. "recordPath": recordPath,
  37. "en": en,
  38. "evaluatorType": evaluatorType ?? SpeechPlugin.evaluatorType,
  39. });
  40. }
  41. /// 初始化listener 进入页面时调用
  42. void initListener() {
  43. _onEvaluatorChange = new StreamController<Map>.broadcast();
  44. _channel.setMethodCallHandler(_onMethodCallHandler);
  45. }
  46. /// 原生返回数据回调方法
  47. /// 录音进度
  48. Future _onMethodCallHandler(MethodCall call) {
  49. switch (call.method) {
  50. case "evaluatorResult":
  51. _onEvaluatorChange.add(call.arguments);
  52. break;
  53. }
  54. return null;
  55. }
  56. /// 关闭stream
  57. void closeStream() {
  58. _channel.setMethodCallHandler(null);
  59. _onEvaluatorChange.close();
  60. }
  61. }