main.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import 'dart:io';
  2. import 'dart:async';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_ali_camera/ali_camera.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:path/path.dart' as path;
  7. void main() async {
  8. WidgetsFlutterBinding.ensureInitialized();
  9. await FlutterAliCameraController.initializeSdk();
  10. runApp(MyApp());
  11. }
  12. class MyApp extends StatefulWidget {
  13. @override
  14. _MyAppState createState() => _MyAppState();
  15. }
  16. class _MyAppState extends State<MyApp> {
  17. FlutterAliCameraController _controller = FlutterAliCameraController();
  18. StreamSubscription _durationSubscription; // 播放进度订阅
  19. final int duration = 5000;
  20. double value = 0;
  21. bool recording = false;
  22. List<String> recordPath = List();
  23. @override
  24. void initState() {
  25. super.initState();
  26. }
  27. @override
  28. void dispose() {
  29. _durationSubscription.cancel();
  30. _controller.dispose();
  31. super.dispose();
  32. }
  33. Future<void> initPlatform() async {
  34. await _controller.create(
  35. recordOption: CameraRecordOption(
  36. videoWidth: 720, videoHeight: 1280, quality: VideoQuality.SD));
  37. _durationSubscription = _controller.recordUpdate.listen((result) {
  38. print("record update" + result.toString());
  39. if (result.containsKey("path")) {
  40. print("录制完成");
  41. setState(() {
  42. recording = false;
  43. recordPath.add(result['path']);
  44. });
  45. }
  46. });
  47. setState(() {});
  48. }
  49. void _onPlatformViewCreated() {
  50. initPlatform();
  51. }
  52. static Future<String> _findLocalPath() async {
  53. final directory = Platform.isAndroid
  54. ? await getExternalStorageDirectory()
  55. : await getApplicationDocumentsDirectory();
  56. return directory.path;
  57. }
  58. Future<void> _record() async {
  59. setState(() {
  60. recording = true;
  61. });
  62. String mp4Path = path.join(await _findLocalPath(), "1.mp4");
  63. _controller.startRecord(duration, mp4Path);
  64. }
  65. void _compose() async {
  66. if (recordPath.length == 0) return;
  67. String resultPath = path.join(await _findLocalPath(), "result.mp4");
  68. bool success = await _controller.startCompose(
  69. resultPath,
  70. null,
  71. recordPath,
  72. recordPath.map((f) => duration).toList(),
  73. CameraComposeOption(
  74. outputWidth: 480,
  75. outputHeight: 480 * 16 ~/ 9,
  76. videoCodecs: VideoCodecs.H264_HARDWARE,
  77. quality: VideoQuality.SD,
  78. frameRate: 20,
  79. bitrate: 500000),
  80. );
  81. print("compose result: ${success ? "successful" : "failed"}");
  82. }
  83. @override
  84. Widget build(BuildContext context) {
  85. return MaterialApp(
  86. home: Scaffold(
  87. backgroundColor: Colors.white,
  88. appBar: AppBar(
  89. title: const Text('Plugin example app'),
  90. ),
  91. body: ListView(
  92. padding: EdgeInsets.symmetric(horizontal: 10),
  93. children: <Widget>[
  94. SizedBox(
  95. width: double.maxFinite,
  96. height: 200,
  97. child: _controller.buildView(_onPlatformViewCreated),
  98. ),
  99. Row(
  100. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  101. children: <Widget>[
  102. IconButton(
  103. icon: Icon(
  104. Icons.play_arrow,
  105. size: 26,
  106. ),
  107. onPressed: () {
  108. if (!recording) _controller.startPreview();
  109. }),
  110. IconButton(
  111. icon: Icon(
  112. Icons.pause,
  113. size: 26,
  114. ),
  115. onPressed: () {
  116. if (!recording) _controller.stopPreview();
  117. }),
  118. IconButton(
  119. icon: Icon(
  120. Icons.switch_camera,
  121. size: 26,
  122. ),
  123. onPressed: () {
  124. if (!recording) _controller.switchCamera();
  125. }),
  126. IconButton(
  127. icon: Icon(
  128. recording ? Icons.stop : Icons.fiber_manual_record,
  129. size: 26,
  130. ),
  131. onPressed: () {
  132. if (!recording) _record();
  133. }),
  134. IconButton(
  135. icon: Icon(
  136. Icons.settings,
  137. size: 26,
  138. ),
  139. onPressed: () {
  140. if (!recording) _compose();
  141. }),
  142. ],
  143. ),
  144. SizedBox(
  145. height: 5,
  146. ),
  147. Row(
  148. children: <Widget>[
  149. Text("美颜0-100"),
  150. Expanded(
  151. child: Slider(
  152. value: value,
  153. max: 100,
  154. onChanged: (value) {
  155. _controller.setBeauty(value.toInt());
  156. setState(() {
  157. this.value = value;
  158. });
  159. }),
  160. ),
  161. ],
  162. ),
  163. ],
  164. ),
  165. ),
  166. );
  167. }
  168. }