speech_plugin.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 SpeechSdk speechSdk = SpeechSdk.IFly;
  11. static SpeechPlugin get instance => _instance;
  12. static Future<String> get platformVersion async {
  13. final String version = await _channel.invokeMethod('getPlatformVersion');
  14. return version;
  15. }
  16. Future<void> initSdk() {
  17. return _channel.invokeMethod("initSpeechSdk");
  18. }
  19. void updateEvaluatorType({int plevType}) {
  20. SpeechPlugin.evaluatorType = plevType; // 1为开启全维度,0为关闭
  21. }
  22. void updateEvaluatorSdkType({SpeechSdk sdkType}) {
  23. SpeechPlugin.speechSdk = sdkType; // 1为开启chivox,0为讯飞
  24. }
  25. Future<Map> evaluatorByAudio(String pathEvaluatorDecode, int index, String videoId, String recordPath, String en, {int evaluatorType}) {
  26. return _channel.invokeMethod("evaluatorByAudio", {
  27. "pathEvaluatorDecode": pathEvaluatorDecode,
  28. "index": index,
  29. "videoId": videoId,
  30. "recordPath": recordPath,
  31. "en": en,
  32. "evaluatorType": evaluatorType ?? SpeechPlugin.evaluatorType,
  33. "sdkType": speechSdk.index,
  34. });
  35. }
  36. Future<Map> evaluatorByMp4(String pathEvaluatorDecode, int index, String videoId, String recordPath, String en, {int evaluatorType}) {
  37. return _channel.invokeMethod("evaluatorByMp4", {
  38. "pathEvaluatorDecode": pathEvaluatorDecode,
  39. "index": index,
  40. "videoId": videoId,
  41. "recordPath": recordPath,
  42. "en": en,
  43. "evaluatorType": evaluatorType ?? SpeechPlugin.evaluatorType,
  44. "sdkType": speechSdk.index,
  45. });
  46. }
  47. /// 初始化listener 进入页面时调用
  48. void initListener() {
  49. _onEvaluatorChange = new StreamController<Map>.broadcast();
  50. _channel.setMethodCallHandler(_onMethodCallHandler);
  51. }
  52. /// 原生返回数据回调方法
  53. /// 录音进度
  54. Future _onMethodCallHandler(MethodCall call) {
  55. switch (call.method) {
  56. case "evaluatorResult":
  57. _onEvaluatorChange.add(call.arguments);
  58. break;
  59. }
  60. return null;
  61. }
  62. /// 关闭stream
  63. void closeStream() {
  64. _channel.setMethodCallHandler(null);
  65. _onEvaluatorChange.close();
  66. }
  67. }
  68. enum SpeechSdk {
  69. IFly, Chivox, SmartOral // 0 讯飞 1 词声 2 腾讯
  70. }