camera_view_controller.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import 'dart:async';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/services.dart';
  4. import 'camera_option.dart';
  5. class CameraViewController {
  6. MethodChannel _methodChannel;
  7. final StreamController<Map> _recordController = StreamController.broadcast();
  8. Stream<Map> get recordUpdate => _recordController.stream;
  9. CameraViewController({@required int id}) {
  10. _methodChannel = MethodChannel("com.i2edu.cameraLib/camera_view_$id");
  11. _methodChannel.setMethodCallHandler(_onMethodCallHandler);
  12. }
  13. Future<void> create({@required CameraRecordOption recordOption, String iosTaskPath}) async {
  14. return _methodChannel.invokeMethod("create",
  15. {"recordOption": recordOption.toMap(), "iosTaskPath": iosTaskPath});
  16. }
  17. Future<void> startPreview() {
  18. return _methodChannel.invokeMethod("startPreview");
  19. }
  20. Future<void> stopPreview() {
  21. return _methodChannel.invokeMethod("stopPreview");
  22. }
  23. Future<void> switchCamera() {
  24. return _methodChannel.invokeMethod("switchCamera");
  25. }
  26. Future<void> setBeauty(int level) {
  27. return _methodChannel.invokeMethod("setBeauty", {"level": level});
  28. }
  29. Future<void> setFilter(String path) {
  30. return _methodChannel.invokeMethod("setFilter", {"path": path});
  31. }
  32. Future<void> startRecord(int max, String recordPath) {
  33. return _methodChannel.invokeMethod("startRecord", {"max": max, "recordPath": recordPath});
  34. }
  35. Future<bool> startCompose(String outputPath, String bgmPath,
  36. List<String> paths, List<int> durations, CameraComposeOption option) {
  37. if (paths.length != durations.length) {
  38. print("error: length error");
  39. return null;
  40. }
  41. return _methodChannel.invokeMethod("startCompose", {
  42. "outputPath": outputPath,
  43. "bgmPath": bgmPath,
  44. "paths": paths,
  45. "durations": durations,
  46. "composeOption": option.toMap()
  47. });
  48. }
  49. void dispose() async {
  50. if (!_recordController.isClosed) {
  51. _recordController.close();
  52. }
  53. return _methodChannel.invokeMethod("onDestroy");
  54. }
  55. Future _onMethodCallHandler(MethodCall call) {
  56. switch (call.method) {
  57. case "recordUpdate":
  58. _recordController.add(call.arguments);
  59. break;
  60. }
  61. return null;
  62. }
  63. }