main.dart 5.6 KB

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