Advertisement
jevixlugya

video player flutter

Apr 15th, 2024
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.57 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:video_player/video_player.dart';
  3.  
  4.  
  5.  
  6. /// Stateful widget to fetch and then display video content.
  7. class VideoApp extends StatefulWidget {
  8.   const VideoApp({super.key});
  9.  
  10.   @override
  11.   _VideoAppState createState() => _VideoAppState();
  12. }
  13.  
  14. class _VideoAppState extends State<VideoApp> {
  15.   late VideoPlayerController _controller;
  16.  
  17.   @override
  18.   void initState() {
  19.     super.initState();
  20.     _controller = VideoPlayerController.networkUrl(Uri.parse(
  21.         'http://youtu.be/G7N4L0Rh0Ro?si=dMnLYbsvuCmJdpTI'))
  22.       ..initialize().then((_) {
  23.         // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
  24.         setState(() {});
  25.       });
  26.   }
  27.  
  28.   @override
  29.   Widget build(BuildContext context) {
  30.     return Scaffold(
  31.         body: Center(
  32.           child: _controller.value.isInitialized
  33.               ? AspectRatio(
  34.             aspectRatio: _controller.value.aspectRatio,
  35.             child: VideoPlayer(_controller),
  36.           )
  37.               : Container(),
  38.         ),
  39.         floatingActionButton: FloatingActionButton(
  40.           onPressed: () {
  41.             setState(() {
  42.               _controller.value.isPlaying
  43.                   ? _controller.pause()
  44.                   : _controller.play();
  45.             });
  46.           },
  47.           child: Icon(
  48.             _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
  49.           ),
  50.         ),
  51.  
  52.     );
  53.   }
  54.  
  55.   @override
  56.   void dispose() {
  57.     _controller.dispose();
  58.     super.dispose();
  59.   }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement