dubbing_lib.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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<String> downLoadVideo(String videoUrl) {
  36. return _channel.invokeMethod("downLoadVideo", {"url": videoUrl});
  37. }
  38. Future<void> pauseRecordAudio() {
  39. return _channel.invokeMethod("pauseRecordAudio");
  40. }
  41. /// 返回录音文件地址
  42. Future<String> startRecord(int index, int duration, String fileName) {
  43. return _channel.invokeMethod("startRecord",
  44. {"duration": duration, "fileName": fileName, "index": index, "pathAudio": recordPath, "pathAudioDecode": recordDecodePath}
  45. );
  46. }
  47. Future<void> playRecordAudio(String filePath) {
  48. return _channel.invokeMethod("playRecordAudio", {"fileName": filePath});
  49. }
  50. /// 返回合成视频文件地址 这里的bgmPath是已经下载的背景音乐文件地址:
  51. Future<String> startMixinAudio(String videoId, String bgmUrl, String bgmPath, List<int> endTimeList,
  52. List<String> decodeAudioPathList, List<int> durationList, String localVideoPath) {
  53. return _channel.invokeMethod("startMixinAudio", {"videoId": videoId, "bgmUrl": bgmUrl,
  54. "endTimeList": endTimeList, "audioDecodePaths": decodeAudioPathList, "durationList": durationList, "videoPath": localVideoPath,
  55. "bgmPath": bgmPath, "pathBgmDecode": bgmDecodePath, "pathBgmRecordSync": audioSyncPath, "pathBgmRecordDecodeSync": audioSyncDecodePath, "pathVideoMixin": videoMixInPath});
  56. }
  57. Future<void> cleanAudioData(String videoId) {
  58. return _channel.invokeMethod("cleanAudioData", {"videoId": videoId, "pathAudio": recordPath, "pathAudioDecode": recordDecodePath});
  59. }
  60. /// 初始化路径
  61. void initPath({@required String videoPath, @required String recordPath, @required String recordDecodePath,
  62. @required String bgmPath, @required String bgmDecodePath, @required String audioSyncPath, @required String audioSyncDecodePath,
  63. @required String videoMixInPath}) {
  64. this.videoPath = videoPath;
  65. this.recordPath = recordPath;
  66. this.recordDecodePath = recordDecodePath;
  67. this.bgmPath = bgmPath;
  68. this.bgmDecodePath = bgmDecodePath;
  69. this.audioSyncPath = audioSyncPath;
  70. this.audioSyncDecodePath = audioSyncDecodePath;
  71. this. videoMixInPath = videoMixInPath;
  72. }
  73. /// 初始化listener 进入录音页面时调用
  74. void initListener() {
  75. _onProgressChange = new StreamController<int>.broadcast();
  76. _onRecordProgressChange = new StreamController<int>.broadcast();
  77. _onDecodeResultChange = new StreamController<Map>.broadcast();
  78. _channel.setMethodCallHandler(_onMethodCallHandler);
  79. }
  80. /// 1,下载视频进度 2.录音进度 3.解码录音回调 4.语音评测结果
  81. Future _onMethodCallHandler(MethodCall call) {
  82. switch (call.method) {
  83. case "downloadUpdate":
  84. _onProgressChange.add(call.arguments['progress']);
  85. break;
  86. case "recordProgress":
  87. _onRecordProgressChange.add(call.arguments['progress']);
  88. break;
  89. case "decodeResult":
  90. _onDecodeResultChange.add(call.arguments);
  91. break;
  92. }
  93. return null;
  94. }
  95. /// 关闭stream
  96. void closeStream() {
  97. _channel.setMethodCallHandler(null);
  98. _onProgressChange.close();
  99. _onRecordProgressChange.close();
  100. _onDecodeResultChange.close();
  101. }
  102. }