-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
179 lines (150 loc) · 4.19 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mvimg/mvimg.dart';
import 'package:mvimg_example/const/resource.dart';
import 'package:path_provider/path_provider.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const PreviewMvimgPage(),
);
}
}
class PreviewMvimgPage extends StatefulWidget {
const PreviewMvimgPage({super.key});
@override
State<PreviewMvimgPage> createState() => _PreviewMvimgPageState();
}
class _PreviewMvimgPageState extends State<PreviewMvimgPage> {
late VideoPlayerController controller;
Uint8List? _imageBytes;
Uint8List? _videoBytes;
@override
void initState() {
super.initState();
initData();
}
Future<void> initData() async {
final data = await rootBundle.load(R.ASSETS_TEST_JPG);
final list = data.buffer.asUint8List();
final mvImage = Mvimg(BufferInput.memory(list));
mvImage.decode();
if (!mvImage.isMvimg()) {
return;
}
final imageBytes = mvImage.getImageBytes();
final videoBytes = mvImage.getVideoBytes();
print('videoBytes length: ${videoBytes.length}');
_imageBytes = Uint8List.fromList(imageBytes);
_videoBytes = Uint8List.fromList(videoBytes);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('preview mvimg'),
),
body: Center(
child: Column(
children: [
const Text('The file is a jpeg file with a motion photo.'),
const Text('Tap to play the motion photo.'),
_buildBody(),
],
)),
);
}
Widget _buildBody() {
final videoBytes = _videoBytes;
final imageBytes = _imageBytes;
if (videoBytes == null || imageBytes == null) {
return const Text('loading...');
}
return VideoAutoPreviewWidget(
videoBytes: videoBytes,
previewBytes: imageBytes,
);
}
}
class VideoAutoPreviewWidget extends StatefulWidget {
const VideoAutoPreviewWidget({
super.key,
required this.videoBytes,
required this.previewBytes,
});
final Uint8List videoBytes;
final Uint8List previewBytes;
@override
State<VideoAutoPreviewWidget> createState() => _VideoAutoPreviewWidgetState();
}
class _VideoAutoPreviewWidgetState extends State<VideoAutoPreviewWidget> {
VideoPlayerController? controller;
@override
void initState() {
super.initState();
initVideo();
}
Future<void> initVideo() async {
final cachePath = await getApplicationCacheDirectory();
final file = File('${cachePath.path}/test.mp4');
await file.writeAsBytes(widget.videoBytes);
final controller = VideoPlayerController.file(file);
this.controller = controller;
await controller.initialize();
setState(() {});
}
@override
Widget build(BuildContext context) {
return buildVideoWidget(controller);
}
Widget buildVideoWidget(VideoPlayerController? controller) {
if (controller == null) {
return Image.memory(widget.previewBytes);
}
return GestureDetector(
onTap: () {
if (controller.value.isPlaying) {
controller.pause();
} else {
controller.play();
}
},
child: ValueListenableBuilder(
valueListenable: controller,
builder: (context, value, w) {
return Stack(
children: [
AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: VideoPlayer(controller),
),
Visibility(
visible: !controller.value.isPlaying,
child: Image.memory(widget.previewBytes),
),
],
);
},
),
);
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
}