Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:video_player/video_player.dart';
- /// 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();
- _controller = VideoPlayerController.networkUrl(Uri.parse(
- 'http://youtu.be/G7N4L0Rh0Ro?si=dMnLYbsvuCmJdpTI'))
- ..initialize().then((_) {
- // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
- setState(() {});
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Center(
- child: _controller.value.isInitialized
- ? AspectRatio(
- aspectRatio: _controller.value.aspectRatio,
- child: VideoPlayer(_controller),
- )
- : Container(),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () {
- setState(() {
- _controller.value.isPlaying
- ? _controller.pause()
- : _controller.play();
- });
- },
- child: Icon(
- _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
- ),
- ),
- );
- }
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement