You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.3KB

  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:video_player/video_player.dart';
  5. class VideoWidget extends StatefulWidget {
  6. final bool play;
  7. final bool isServerFile;
  8. final String pathFile;
  9. const VideoWidget(
  10. {Key key,
  11. @required this.pathFile,
  12. @required this.play,
  13. @required this.isServerFile})
  14. : super(key: key);
  15. @override
  16. _VideoWidgetState createState() => _VideoWidgetState();
  17. }
  18. class _VideoWidgetState extends State<VideoWidget> {
  19. VideoPlayerController videoPlayerController;
  20. Future<void> _initializeVideoPlayerFuture;
  21. @override
  22. void initState() {
  23. super.initState();
  24. if (widget.isServerFile) {
  25. videoPlayerController = VideoPlayerController.network(widget.pathFile);
  26. } else {
  27. videoPlayerController = VideoPlayerController.file(File(widget.pathFile));
  28. }
  29. _initializeVideoPlayerFuture = videoPlayerController.initialize().then((_) {
  30. // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
  31. videoPlayerController.play();
  32. videoPlayerController.setVolume(0.0);
  33. Timer.periodic(Duration(seconds: 1), (_) {
  34. videoPlayerController.pause();
  35. });
  36. setState(() {});
  37. });
  38. }
  39. @override
  40. void dispose() {
  41. videoPlayerController.dispose();
  42. print("dispose video item");
  43. super.dispose();
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. return FutureBuilder(
  48. future: _initializeVideoPlayerFuture,
  49. builder: (context, snapshot) {
  50. if (snapshot.connectionState == ConnectionState.done) {
  51. return new Container(
  52. child: Container(
  53. key: new PageStorageKey(widget.pathFile),
  54. child: Container(
  55. padding: EdgeInsets.all(1),
  56. width: 100,
  57. height: 100,
  58. decoration: BoxDecoration(
  59. color: Colors.white,
  60. border: Border.all(color: Colors.red),
  61. borderRadius: BorderRadius.all(Radius.circular(8.0))),
  62. child: VideoPlayer(videoPlayerController),
  63. ),
  64. ),
  65. );
  66. } else {
  67. return Center(
  68. child: CircularProgressIndicator(),
  69. );
  70. }
  71. },
  72. );
  73. }
  74. }