ali_camera_controller.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_ali_camera/camera_option.dart';
  7. class FlutterAliCameraController {
  8. static const MethodChannel _channel =
  9. const MethodChannel('flutter_ali_camera');
  10. static const String viewType = "com.i2edu.cameraLib";
  11. final StreamController<Map> _recordController = StreamController.broadcast();
  12. Stream<Map> get recordUpdate => _recordController.stream;
  13. static Future<String> get platformVersion async {
  14. final String version = await _channel.invokeMethod('getPlatformVersion');
  15. return version;
  16. }
  17. static Future<void> initializeSdk() {
  18. return _channel.invokeMethod("initializeSdk");
  19. }
  20. Future<void> create({@required CameraRecordOption recordOption}) async {
  21. _channel.setMethodCallHandler(_onMethodCallHandler);
  22. return await _channel.invokeMethod("create", {"recordOption": recordOption.toMap()});
  23. }
  24. Widget buildView(VoidCallback onPlatformViewCreated) {
  25. return Platform.isAndroid
  26. ? AndroidView(
  27. viewType: viewType,
  28. creationParams: {},
  29. creationParamsCodec: const StandardMessageCodec(),
  30. onPlatformViewCreated: (id) => onPlatformViewCreated(),
  31. )
  32. : UiKitView(
  33. viewType: viewType,
  34. creationParams: {},
  35. creationParamsCodec: const StandardMessageCodec(),
  36. onPlatformViewCreated: (id) => onPlatformViewCreated(),
  37. );
  38. }
  39. Future<void> startPreview() {
  40. return _channel.invokeMethod("startPreview");
  41. }
  42. Future<void> stopPreview() {
  43. return _channel.invokeMethod("stopPreview");
  44. }
  45. Future<void> switchCamera() {
  46. return _channel.invokeMethod("switchCamera");
  47. }
  48. Future<void> setBeauty(int level) {
  49. return _channel.invokeMethod("setBeauty", {"level": level});
  50. }
  51. Future<void> setFilter(String path) {
  52. return _channel.invokeMethod("setFilter", {"path": path});
  53. }
  54. Future<void> startRecord(int max, String recordPath) {
  55. return _channel.invokeMethod("startRecord", {"max": max, "recordPath": recordPath});
  56. }
  57. Future<bool> startCompose(String outputPath, String bgmPath,
  58. List<String> paths, List<int> durations, CameraComposeOption option) {
  59. if (paths.length != durations.length) {
  60. print("error: length error");
  61. return null;
  62. }
  63. return _channel.invokeMethod("startCompose", {
  64. "outputPath": outputPath,
  65. "bgmPath": bgmPath,
  66. "paths": paths,
  67. "durations": durations,
  68. "composeOption": option.toMap()
  69. });
  70. }
  71. Future _onMethodCallHandler(MethodCall call) {
  72. print(call.arguments);
  73. switch (call.method) {
  74. case "recordUpdate":
  75. _recordController.add(call.arguments);
  76. break;
  77. }
  78. return null;
  79. }
  80. Future<void> dispose() async {
  81. return _channel.invokeMethod("onDestroy");
  82. }
  83. }