dubbing_lib.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. Stream<int> get recordProgressChange => _onRecordProgressChange.stream;
  13. Stream<Map> get decodeResultChange => _onDecodeResultChange.stream;
  14. /// 路径文件夹地址
  15. String videoPath;
  16. String recordPath;
  17. String recordDecodePath;
  18. String bgmPath;
  19. String bgmDecodePath;
  20. String audioSyncPath;
  21. String audioSyncDecodePath;
  22. String videoMixInPath;
  23. static Future<String> get platformVersion async {
  24. final String version = await _channel.invokeMethod('getPlatformVersion');
  25. return version;
  26. }
  27. Future<void> init() {
  28. return _channel.invokeMethod("initSpeechSdk");
  29. }
  30. Future<void> setExtraFullScreen() {
  31. //todo 暂时视频播放时全屏StatusBar bug
  32. return _channel.invokeMethod("setExtraFullScreen");
  33. }
  34. Future<String> downLoadVideo(String videoUrl) {
  35. return _channel.invokeMethod("downLoadVideo", {"url": videoUrl});
  36. }
  37. Future<void> pauseRecordAudio() {
  38. return _channel.invokeMethod("pauseRecordAudio");
  39. }
  40. /// 返回录音文件地址
  41. Future<String> startRecord(int index, int duration, String fileName) {
  42. return _channel.invokeMethod("startRecord",
  43. {
  44. "duration": duration,
  45. "fileName": fileName,
  46. "index": index,
  47. "pathAudio": recordPath,
  48. "pathAudioDecode": recordDecodePath
  49. }
  50. );
  51. }
  52. Future<void> playRecordAudio(String filePath) {
  53. return _channel.invokeMethod("playRecordAudio", {"fileName": filePath});
  54. }
  55. Future<String> startMixinAudio(String videoId, String bgmUrl, String bgmPath,
  56. List<int> endTimeList,
  57. List<String> decodeAudioPathList,
  58. List<int> durationList, String localVideoPath,
  59. List<double> startTimeList, String mixinName) {
  60. return _channel.invokeMethod(
  61. "startMixinAudio", {
  62. "videoId": videoId,
  63. "bgmUrl": bgmUrl,
  64. "endTimeList": endTimeList,
  65. "audioDecodePaths": decodeAudioPathList,
  66. "durationList": durationList,
  67. "videoPath": localVideoPath,
  68. "bgmPath": bgmPath,
  69. "pathBgmDecode": bgmDecodePath,
  70. "pathBgmRecordSync": audioSyncPath,
  71. "pathBgmRecordDecodeSync": audioSyncDecodePath,
  72. "pathVideoMixin": videoMixInPath,
  73. "startTimeList": startTimeList,
  74. "mixinName": mixinName
  75. });
  76. }
  77. Future<void> cleanAudioData(String videoId) {
  78. return _channel.invokeMethod("cleanAudioData", {
  79. "videoId": videoId,
  80. "pathAudio": recordPath,
  81. "pathAudioDecode": recordDecodePath
  82. });
  83. }
  84. Future<String> startMixinPaintedAudio(List<String> audioPaths, String bgmPath, List<int> durationList, List<int> endTimeList,
  85. String audioDecodePath, String mixinFilePath, String encodePath,List<int> startTimeList) {
  86. return _channel.invokeMethod("startMixinPaintedAudio",
  87. {"audioPaths": audioPaths, "bgmPath": bgmPath, "durationList": durationList, "endTimeList": endTimeList,
  88. "audioDecodePath": audioDecodePath, "mixinFilePath": mixinFilePath, "encodePath": encodePath,"startTimeList": startTimeList});
  89. }
  90. /// 初始化路径
  91. void initPath(
  92. {@required String videoPath, @required String recordPath, @required String recordDecodePath,
  93. @required String bgmPath, @required String bgmDecodePath, @required String audioSyncPath, @required String audioSyncDecodePath,
  94. @required String videoMixInPath}) {
  95. this.videoPath = videoPath;
  96. this.recordPath = recordPath;
  97. this.recordDecodePath = recordDecodePath;
  98. this.bgmPath = bgmPath;
  99. this.bgmDecodePath = bgmDecodePath;
  100. this.audioSyncPath = audioSyncPath;
  101. this.audioSyncDecodePath = audioSyncDecodePath;
  102. this.videoMixInPath = videoMixInPath;
  103. }
  104. /// 初始化listener 进入录音页面时调用
  105. void initListener() {
  106. _onProgressChange = new StreamController<int>.broadcast();
  107. _onRecordProgressChange = new StreamController<int>.broadcast();
  108. _onDecodeResultChange = new StreamController<Map>.broadcast();
  109. _channel.setMethodCallHandler(_onMethodCallHandler);
  110. }
  111. /// 1.录音进度 2.解码录音回调
  112. Future _onMethodCallHandler(MethodCall call) {
  113. switch (call.method) {
  114. case "recordProgress":
  115. _onRecordProgressChange.add(call.arguments['progress']);
  116. break;
  117. case "decodeResult":
  118. _onDecodeResultChange.add(call.arguments);
  119. break;
  120. }
  121. return null;
  122. }
  123. /// 关闭stream
  124. void closeStream() {
  125. _channel.setMethodCallHandler(null);
  126. _onProgressChange.close();
  127. _onRecordProgressChange.close();
  128. _onDecodeResultChange.close();
  129. }
  130. }