import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_ali_camera/camera_option.dart'; class FlutterAliCameraController { static const MethodChannel _channel = const MethodChannel('flutter_ali_camera'); static const String viewType = "com.i2edu.cameraLib"; final StreamController _recordController = StreamController.broadcast(); Stream get recordUpdate => _recordController.stream; static Future get platformVersion async { final String version = await _channel.invokeMethod('getPlatformVersion'); return version; } static Future initializeSdk() { return _channel.invokeMethod("initializeSdk"); } Future create({@required CameraRecordOption recordOption}) async { _channel.setMethodCallHandler(_onMethodCallHandler); return await _channel.invokeMethod("create", {"recordOption": recordOption.toMap()}); } Widget buildView(VoidCallback onPlatformViewCreated) { return Platform.isAndroid ? AndroidView( viewType: viewType, creationParams: {}, creationParamsCodec: const StandardMessageCodec(), onPlatformViewCreated: (id) => onPlatformViewCreated(), ) : UiKitView( viewType: viewType, creationParams: {}, creationParamsCodec: const StandardMessageCodec(), onPlatformViewCreated: (id) => onPlatformViewCreated(), ); } Future startPreview() { return _channel.invokeMethod("startPreview"); } Future stopPreview() { return _channel.invokeMethod("stopPreview"); } Future switchCamera() { return _channel.invokeMethod("switchCamera"); } Future setBeauty(int level) { return _channel.invokeMethod("setBeauty", {"level": level}); } Future setFilter(String path) { return _channel.invokeMethod("setFilter", {"path": path}); } Future startRecord(int max, String recordPath) { return _channel.invokeMethod("startRecord", {"max": max, "recordPath": recordPath}); } Future startCompose(String outputPath, String bgmPath, List paths, List durations, CameraComposeOption option) { if (paths.length != durations.length) { print("error: length error"); return null; } return _channel.invokeMethod("startCompose", { "outputPath": outputPath, "bgmPath": bgmPath, "paths": paths, "durations": durations, "composeOption": option.toMap() }); } Future _onMethodCallHandler(MethodCall call) { print(call.arguments); switch (call.method) { case "recordUpdate": _recordController.add(call.arguments); break; } return null; } Future dispose() async { return _channel.invokeMethod("onDestroy"); } }