File size: 11,407 Bytes
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file is used to extract code samples for the README.md file.
// Run update-excerpts if you modify this file.

// ignore_for_file: library_private_types_in_public_api, public_member_api_docs

// #docregion basic-example
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
import 'package:fvp/fvp.dart';
import 'package:logging/logging.dart';
import 'package:intl/intl.dart';
import 'package:file_selector/file_selector.dart';
import 'package:image/image.dart' as img;

String? source;

void main(List<String> args) async {
// set logger before registerWith()
  Logger.root.level = Level.ALL;
  final df = DateFormat("HH:mm:ss.SSS");
  Logger.root.onRecord.listen((record) {
    debugPrint(
        '${record.loggerName}.${record.level.name}: ${df.format(record.time)}: ${record.message}',
        wrapWidth: 0x7FFFFFFFFFFFFFFF);
  });

  final opts = <String, Object>{};
  final globalOpts = <String, Object>{};
  int i = 0;
  bool useFvp = true;
  opts['subtitleFontFile'] =
      'https://github.com/mpv-android/mpv-android/raw/master/app/src/main/assets/subfont.ttf';
  for (; i < args.length; i++) {
    if (args[i] == '-c:v') {
      opts['video.decoders'] = [args[++i]];
    } else if (args[i] == '-maxSize') {
      // ${w}x${h}
      final size = args[++i].split('x');
      opts['maxWidth'] = int.parse(size[0]);
      opts['maxHeight'] = int.parse(size[1]);
    } else if (args[i] == '-fvp') {
      useFvp = int.parse(args[++i]) > 0;
    } else if (args[i].startsWith('-')) {
      globalOpts[args[i].substring(1)] = args[++i];
    } else {
      break;
    }
  }
  if (globalOpts.isNotEmpty) {
    opts['global'] = globalOpts;
  }
  opts['lowLatency'] = 0;

  if (i <= args.length - 1) source = args[args.length - 1];
  source ??= await getStartFile();

  if (useFvp) {
    registerWith(options: opts);
  }

  runApp(const MaterialApp(
      localizationsDelegates: [DefaultMaterialLocalizations.delegate],
      title: 'Video Demo',
      home: VideoApp()));
}

Future<String?> getStartFile() async {
  if (Platform.isMacOS) {
    WidgetsFlutterBinding.ensureInitialized();
    const hostApi = MethodChannel("openFile");
    return await hostApi.invokeMethod("getCurrentFile");
  }
  return null;
}

Future<String?> showURIPicker(BuildContext context) async {
  final key = GlobalKey<FormState>();
  final src = TextEditingController();
  String? uri;
  await showModalBottomSheet(
    context: context,
    builder: (context) => Container(
      alignment: Alignment.center,
      child: Form(
        key: key,
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              TextFormField(
                controller: src,
                style: const TextStyle(fontSize: 14.0),
                decoration: const InputDecoration(
                  border: UnderlineInputBorder(),
                  labelText: 'Video URI',
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter a URI';
                  }
                  return null;
                },
              ),
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 16.0),
                child: ElevatedButton(
                  onPressed: () {
                    if (key.currentState!.validate()) {
                      uri = src.text;
                      Navigator.of(context).maybePop();
                    }
                  },
                  child: const Text('Play'),
                ),
              ),
            ],
          ),
        ),
      ),
    ),
  );
  return uri;
}

/// Stateful widget to fetch and then display video content.
class VideoApp extends StatefulWidget {
  const VideoApp({super.key});

  @override
  _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoApp> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    if (source != null) {
      if (File(source!).existsSync()) {
        playFile(source!);
      } else {
        playUri(source!);
      }
    } else {
      playUri(
          'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4');
    }
  }

  void playFile(String path) {
    _controller.dispose();
    _controller = VideoPlayerController.file(File(path));
    _controller.addListener(() {
      setState(() {});
    });
    _controller.initialize().then((_) {
      // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
      setState(() {});
      _controller.play();
    });
  }

  void playUri(String uri) {
    _controller = VideoPlayerController.network(uri);
    _controller.addListener(() {
      setState(() {});
    });
    _controller.initialize().then((_) {
      // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
      setState(() {});
      _controller.play();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: Row(
        children: [
          FloatingActionButton(
            heroTag: 'file',
            tooltip: 'Open [File]',
            onPressed: () async {
              const XTypeGroup typeGroup = XTypeGroup(
                label: 'videos',
                //extensions: <String>['*'],
              );
              final XFile? file =
                  await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);

              if (file != null) {
                playFile(file.path);
              }
            },
            child: const Icon(Icons.file_open),
          ),
          const SizedBox(width: 16.0),
          FloatingActionButton(
            heroTag: 'uri',
            tooltip: 'Open [Uri]',
            onPressed: () {
              showURIPicker(context).then((value) {
                if (value != null) {
                  playUri(value);
                }
              });
            },
            child: const Icon(Icons.link),
          ),
          const SizedBox(width: 16.0),
          FloatingActionButton(
            heroTag: 'snapshot',
            tooltip: 'Snapshot',
            onPressed: () {
              if (_controller.value.isInitialized) {
                final info = _controller.getMediaInfo()?.video?[0].codec;
                if (info == null) {
                  debugPrint('No video codec info');
                  return;
                }
                final width = info.width;
                final height = info.height;
                _controller.snapshot().then((value) {
                  if (value != null) {
                    // value is rgba data, must encode to png image and save as a file
                    final i = img.Image.fromBytes(
                        width: width,
                        height: height,
                        bytes: value.buffer,
                        numChannels: 4,
                        rowStride: width * 4);
                    final savePath =
                        '${Directory.systemTemp.path}/snapshot.jpg';
                    img.encodeJpgFile(savePath, i, quality: 70).then((value) {
                      final msg = value
                          ? 'Snapshot saved to $savePath'
                          : 'Failed to save snapshot';
                      debugPrint(msg);
                      // show a toast
                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(
                            content: Text(msg),
                            duration: const Duration(seconds: 2),
                          ),
                        );
                      }
                    });
                  }
                });
              }
            },
            child: const Icon(Icons.screenshot),
          ),
        ],
      ),
      body: Center(
        child: _controller.value.isInitialized
            ? AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: Stack(
                  alignment: Alignment.bottomCenter,
                  children: <Widget>[
                    VideoPlayer(_controller),
                    _ControlsOverlay(controller: _controller),
                    VideoProgressIndicator(_controller, allowScrubbing: true),
                  ],
                ),
              )
            : Container(),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}

class _ControlsOverlay extends StatelessWidget {
  const _ControlsOverlay({required this.controller});

  static const List<double> _examplePlaybackRates = <double>[
    0.25,
    0.5,
    1.0,
    1.5,
    2.0,
    3.0,
    5.0,
    10.0,
  ];

  final VideoPlayerController controller;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        AnimatedSwitcher(
          duration: const Duration(milliseconds: 50),
          reverseDuration: const Duration(milliseconds: 200),
          child: controller.value.isPlaying
              ? const SizedBox.shrink()
              : Container(
                  color: Colors.black26,
                  child: const Center(
                    child: Icon(
                      Icons.play_arrow,
                      color: Colors.white,
                      size: 100.0,
                      semanticLabel: 'Play',
                    ),
                  ),
                ),
        ),
        GestureDetector(
          onTap: () {
            controller.value.isPlaying ? controller.pause() : controller.play();
          },
        ),
        Align(
          alignment: Alignment.topRight,
          child: PopupMenuButton<double>(
            initialValue: controller.value.playbackSpeed,
            tooltip: 'Playback speed',
            onSelected: (double speed) {
              controller.setPlaybackSpeed(speed);
            },
            itemBuilder: (BuildContext context) {
              return <PopupMenuItem<double>>[
                for (final double speed in _examplePlaybackRates)
                  PopupMenuItem<double>(
                    value: speed,
                    child: Text('${speed}x'),
                  )
              ];
            },
            child: Padding(
              padding: const EdgeInsets.symmetric(
                // Using less vertical padding as the text is also longer
                // horizontally, so it feels like it would need more spacing
                // horizontally (matching the aspect ratio of the video).
                vertical: 12,
                horizontal: 16,
              ),
              child: Text('${controller.value.playbackSpeed}x'),
            ),
          ),
        ),
      ],
    );
  }
}

// #enddocregion basic-example