dubbing_lib.dart 4.6 KB

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