import 'dart:io'; import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_ali_camera/ali_camera.dart'; import 'package:path_provider/path_provider.dart'; import 'package:path/path.dart' as path; void main() async { WidgetsFlutterBinding.ensureInitialized(); await FlutterAliCameraController.initializeSdk(); runApp(MyApp()); } class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { FlutterAliCameraController _controller = FlutterAliCameraController(); StreamSubscription _durationSubscription; // 播放进度订阅 final int duration = 5000; double value = 0; bool recording = false; List recordPath = List(); @override void initState() { super.initState(); } @override void dispose() { _durationSubscription.cancel(); _controller.dispose(); super.dispose(); } Future initPlatform() async { await _controller.create( recordOption: CameraRecordOption( videoWidth: 720, videoHeight: 1280, quality: VideoQuality.SD)); _durationSubscription = _controller.recordUpdate.listen((result) { print("record update" + result.toString()); if (result.containsKey("path")) { print("录制完成"); setState(() { recording = false; recordPath.add(result['path']); }); } }); setState(() {}); } void _onPlatformViewCreated() { initPlatform(); } static Future _findLocalPath() async { final directory = Platform.isAndroid ? await getExternalStorageDirectory() : await getApplicationDocumentsDirectory(); return directory.path; } Future _record() async { setState(() { recording = true; }); String mp4Path = path.join(await _findLocalPath(), "1.mp4"); _controller.startRecord(duration, mp4Path); } void _compose() async { if (recordPath.length == 0) return; String resultPath = path.join(await _findLocalPath(), "result.mp4"); bool success = await _controller.startCompose( resultPath, null, recordPath, recordPath.map((f) => duration).toList(), CameraComposeOption( outputWidth: 480, outputHeight: 480 * 16 ~/ 9, videoCodecs: VideoCodecs.H264_HARDWARE, quality: VideoQuality.SD, frameRate: 20, bitrate: 500000), ); print("compose result: ${success ? "successful" : "failed"}"); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: const Text('Plugin example app'), ), body: ListView( padding: EdgeInsets.symmetric(horizontal: 10), children: [ SizedBox( width: double.maxFinite, height: 200, child: _controller.buildView(_onPlatformViewCreated), ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ IconButton( icon: Icon( Icons.play_arrow, size: 26, ), onPressed: () { if (!recording) _controller.startPreview(); }), IconButton( icon: Icon( Icons.pause, size: 26, ), onPressed: () { if (!recording) _controller.stopPreview(); }), IconButton( icon: Icon( Icons.switch_camera, size: 26, ), onPressed: () { if (!recording) _controller.switchCamera(); }), IconButton( icon: Icon( recording ? Icons.stop : Icons.fiber_manual_record, size: 26, ), onPressed: () { if (!recording) _record(); }), IconButton( icon: Icon( Icons.settings, size: 26, ), onPressed: () { if (!recording) _compose(); }), ], ), SizedBox( height: 5, ), Row( children: [ Text("美颜0-100"), Expanded( child: Slider( value: value, max: 100, onChanged: (value) { _controller.setBeauty(value.toInt()); setState(() { this.value = value; }); }), ), ], ), ], ), ), ); } }