dubbing_lib.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. Future<String> startMixinAudio(String videoId, String bgmUrl, String bgmPath, List<int> endTimeList,
  51. List<String> decodeAudioPathList, List<String> audioPathList, List<int> durationList, String localVideoPath, List<double> startTimeList) {
  52. return _channel.invokeMethod("startMixinAudio", {"videoId": videoId, "bgmUrl": bgmUrl,
  53. "endTimeList": endTimeList, "audioDecodePaths": decodeAudioPathList, "audioPathList": audioPathList, "durationList": durationList, "videoPath": localVideoPath,
  54. "bgmPath": bgmPath, "pathBgmDecode": bgmDecodePath, "pathBgmRecordSync": audioSyncPath, "pathBgmRecordDecodeSync": audioSyncDecodePath, "pathVideoMixin": videoMixInPath, "startTimeList": startTimeList});
  55. }
  56. Future<void> cleanAudioData(String videoId) {
  57. return _channel.invokeMethod("cleanAudioData", {"videoId": videoId, "pathAudio": recordPath, "pathAudioDecode": recordDecodePath});
  58. }
  59. /// 初始化路径
  60. void initPath({@required String videoPath, @required String recordPath, @required String recordDecodePath,
  61. @required String bgmPath, @required String bgmDecodePath, @required String audioSyncPath, @required String audioSyncDecodePath,
  62. @required String videoMixInPath}) {
  63. this.videoPath = videoPath;
  64. this.recordPath = recordPath;
  65. this.recordDecodePath = recordDecodePath;
  66. this.bgmPath = bgmPath;
  67. this.bgmDecodePath = bgmDecodePath;
  68. this.audioSyncPath = audioSyncPath;
  69. this.audioSyncDecodePath = audioSyncDecodePath;
  70. this. videoMixInPath = videoMixInPath;
  71. }
  72. /// 初始化listener 进入录音页面时调用
  73. void initListener() {
  74. _onProgressChange = new StreamController<int>.broadcast();
  75. _onRecordProgressChange = new StreamController<int>.broadcast();
  76. _onDecodeResultChange = new StreamController<Map>.broadcast();
  77. _channel.setMethodCallHandler(_onMethodCallHandler);
  78. }
  79. /// 1,下载视频进度 2.录音进度 3.解码录音回调 4.语音评测结果
  80. Future _onMethodCallHandler(MethodCall call) {
  81. switch (call.method) {
  82. case "downloadUpdate":
  83. _onProgressChange.add(call.arguments['progress']);
  84. break;
  85. case "recordProgress":
  86. _onRecordProgressChange.add(call.arguments['progress']);
  87. break;
  88. case "decodeResult":
  89. _onDecodeResultChange.add(call.arguments);
  90. break;
  91. }
  92. return null;
  93. }
  94. /// 关闭stream
  95. void closeStream() {
  96. _channel.setMethodCallHandler(null);
  97. _onProgressChange.close();
  98. _onRecordProgressChange.close();
  99. _onDecodeResultChange.close();
  100. }
  101. }