speech_plugin.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 SpeechPlugin get instance => _instance;
  10. static Future<String> get platformVersion async {
  11. final String version = await _channel.invokeMethod('getPlatformVersion');
  12. return version;
  13. }
  14. Future<void> initSdk() {
  15. return _channel.invokeMethod("initSpeechSdk");
  16. }
  17. Future<Map> evaluatorByAudio(String pathEvaluatorDecode, int index, String videoId, String recordPath, String en) {
  18. return _channel.invokeMethod("evaluatorByAudio", {
  19. "pathEvaluatorDecode": pathEvaluatorDecode,
  20. "index": index,
  21. "videoId": videoId,
  22. "recordPath": recordPath,
  23. "en": en
  24. });
  25. }
  26. Future<Map> evaluatorByMp4(String pathEvaluatorDecode, int index, String videoId, String recordPath, String en) {
  27. return _channel.invokeMethod("evaluatorByMp4", {
  28. "pathEvaluatorDecode": pathEvaluatorDecode,
  29. "index": index,
  30. "videoId": videoId,
  31. "recordPath": recordPath,
  32. "en": en
  33. });
  34. }
  35. /// 初始化listener 进入页面时调用
  36. void initListener() {
  37. _onEvaluatorChange = new StreamController<Map>.broadcast();
  38. _channel.setMethodCallHandler(_onMethodCallHandler);
  39. }
  40. /// 原生返回数据回调方法
  41. /// 录音进度
  42. Future _onMethodCallHandler(MethodCall call) {
  43. switch (call.method) {
  44. case "evaluatorResult":
  45. _onEvaluatorChange.add(call.arguments);
  46. break;
  47. }
  48. return null;
  49. }
  50. /// 关闭stream
  51. void closeStream() {
  52. _channel.setMethodCallHandler(null);
  53. _onEvaluatorChange.close();
  54. }
  55. }