camera_view_controller.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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}) async {
  14. return _methodChannel.invokeMethod("create", {"recordOption": recordOption.toMap()});
  15. }
  16. Future<void> startPreview() {
  17. return _methodChannel.invokeMethod("startPreview");
  18. }
  19. Future<void> stopPreview() {
  20. return _methodChannel.invokeMethod("stopPreview");
  21. }
  22. Future<void> switchCamera() {
  23. return _methodChannel.invokeMethod("switchCamera");
  24. }
  25. Future<void> setBeauty(int level) {
  26. return _methodChannel.invokeMethod("setBeauty", {"level": level});
  27. }
  28. Future<void> setFilter(String path) {
  29. return _methodChannel.invokeMethod("setFilter", {"path": path});
  30. }
  31. Future<void> startRecord(int max, String recordPath) {
  32. return _methodChannel.invokeMethod("startRecord", {"max": max, "recordPath": recordPath});
  33. }
  34. Future<bool> startCompose(String outputPath, String bgmPath,
  35. List<String> paths, List<int> durations, CameraComposeOption option) {
  36. if (paths.length != durations.length) {
  37. print("error: length error");
  38. return null;
  39. }
  40. return _methodChannel.invokeMethod("startCompose", {
  41. "outputPath": outputPath,
  42. "bgmPath": bgmPath,
  43. "paths": paths,
  44. "durations": durations,
  45. "composeOption": option.toMap()
  46. });
  47. }
  48. void dispose() async {
  49. if (!_recordController.isClosed) {
  50. _recordController.close();
  51. }
  52. }
  53. Future _onMethodCallHandler(MethodCall call) {
  54. switch (call.method) {
  55. case "recordUpdate":
  56. _recordController.add(call.arguments);
  57. break;
  58. }
  59. return null;
  60. }
  61. }