dubbing_lib.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. class DubbingLib {
  5. static final DubbingLib _instance = DubbingLib();
  6. static DubbingLib get instance => _instance;
  7. static const MethodChannel _channel =
  8. const MethodChannel('dubbing_lib');
  9. StreamController _onProgressChange;
  10. StreamController _onRecordProgressChange;
  11. StreamController _onDecodeResultChange;
  12. StreamController _onEvaluatorResultChange;
  13. Stream<int> get progressChange => _onProgressChange.stream;
  14. Stream<int> get recordProgressChange => _onRecordProgressChange.stream;
  15. Stream<Map> get decodeResultChange => _onDecodeResultChange.stream;
  16. Stream<Map> get evaluatorResultChange => _onEvaluatorResultChange.stream;
  17. /// 路径文件夹地址
  18. String videoPath;
  19. String recordPath;
  20. String recordDecodePath;
  21. String recordEvaluatorPath;
  22. String bgmPath;
  23. String bgmDecodePath;
  24. String audioSyncPath;
  25. String audioSyncDecodePath;
  26. String videoMixInPath;
  27. static Future<String> get platformVersion async {
  28. final String version = await _channel.invokeMethod('getPlatformVersion');
  29. return version;
  30. }
  31. Future<void> init() {
  32. return _channel.invokeMethod("initSpeechSdk");
  33. }
  34. Future<void> setExtraFullScreen() {
  35. //todo 暂时视频播放时全屏StatusBar bug
  36. return _channel.invokeMethod("setExtraFullScreen");
  37. }
  38. Future<Map<dynamic, dynamic>> findIsExistCacheVideo(String videoId, {String recordPath, String recordDecodePath}) {
  39. return _channel.invokeMethod<Map<dynamic, dynamic>>("findIsExistCacheVideo", {"videoId": videoId, "pathAudio": recordPath ?? this.recordPath, "pathAudioDecode": recordDecodePath ?? this.recordDecodePath});
  40. }
  41. Future<String> downLoadVideo(String videoUrl) {
  42. return _channel.invokeMethod("downLoadVideo", {"url": videoUrl});
  43. }
  44. Future<void> pauseRecordAudio() {
  45. return _channel.invokeMethod("pauseRecordAudio");
  46. }
  47. /// 返回录音文件地址
  48. Future<String> startRecord(int index, int duration, String fileName, String content) {
  49. return _channel.invokeMethod("startRecord",
  50. {"duration": duration, "fileName": fileName, "index": index, "content": content, "pathAudio": recordPath, "pathAudioDecode": recordDecodePath, "pathEvaluatorDecode": recordEvaluatorPath}
  51. );
  52. }
  53. Future<void> playRecordAudio(String filePath) {
  54. return _channel.invokeMethod("playRecordAudio", {"fileName": filePath});
  55. }
  56. /// 返回合成视频文件地址 这里的bgmPath是已经下载的背景音乐文件地址
  57. Future<String> startMixinAudio(String videoId, String bgmUrl, String bgmPath, List<int> endTimeList,
  58. List<String> decodeAudioPathList, List<int> durationList, String localVideoPath) {
  59. return _channel.invokeMethod("startMixinAudio", {"videoId": videoId, "bgmUrl": bgmUrl,
  60. "endTimeList": endTimeList, "audioDecodePaths": decodeAudioPathList, "durationList": durationList, "videoPath": localVideoPath,
  61. "bgmPath": bgmPath, "pathBgmDecode": bgmDecodePath, "pathBgmRecordSync": audioSyncPath, "pathBgmRecordDecodeSync": audioSyncDecodePath, "pathVideoMixin": videoMixInPath});
  62. }
  63. Future<void> cleanAudioData(String videoId) {
  64. return _channel.invokeMethod("cleanAudioData", {"videoId": videoId, "pathAudio": recordPath, "pathAudioDecode": recordDecodePath});
  65. }
  66. /// 初始化路径
  67. void initPath({@required String videoPath, @required String recordPath, @required String recordDecodePath, @required String recordEvaluatorPath,
  68. @required String bgmPath, @required String bgmDecodePath, @required String audioSyncPath, @required String audioSyncDecodePath,
  69. @required String videoMixInPath}) {
  70. this.videoPath = videoPath;
  71. this.recordPath = recordPath;
  72. this.recordDecodePath = recordDecodePath;
  73. this.recordEvaluatorPath = recordEvaluatorPath;
  74. this.bgmPath = bgmPath;
  75. this.bgmDecodePath = bgmDecodePath;
  76. this.audioSyncPath = audioSyncPath;
  77. this.audioSyncDecodePath = audioSyncDecodePath;
  78. this. videoMixInPath = videoMixInPath;
  79. }
  80. /// 初始化listener 进入录音页面时调用
  81. void initListener() {
  82. _onProgressChange = new StreamController<int>.broadcast();
  83. _onRecordProgressChange = new StreamController<int>.broadcast();
  84. _onDecodeResultChange = new StreamController<Map>.broadcast();
  85. _onEvaluatorResultChange = new StreamController<Map>.broadcast();
  86. _channel.setMethodCallHandler(_onMethodCallHandler);
  87. }
  88. /// 1,下载视频进度 2.录音进度 3.解码录音回调 4.语音评测结果
  89. Future _onMethodCallHandler(MethodCall call) {
  90. switch (call.method) {
  91. case "downloadUpdate":
  92. _onProgressChange.add(call.arguments['progress']);
  93. break;
  94. case "recordProgress":
  95. _onRecordProgressChange.add(call.arguments['progress']);
  96. break;
  97. case "decodeResult":
  98. _onDecodeResultChange.add(call.arguments);
  99. break;
  100. case "evaluatorResult":
  101. _onEvaluatorResultChange.add(call.arguments);
  102. break;
  103. }
  104. return null;
  105. }
  106. /// 关闭stream
  107. void closeStream() {
  108. _channel.setMethodCallHandler(null);
  109. _onProgressChange.close();
  110. _onRecordProgressChange.close();
  111. _onDecodeResultChange.close();
  112. _onEvaluatorResultChange.close();
  113. }
  114. }