Last active
November 26, 2024 21:53
-
-
Save iapicca/cb19ca180e68800ec0ddc4d10e8c618a to your computer and use it in GitHub Desktop.
issue_159503
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:video_player/video_player.dart'; | |
| void main() => runApp(const MyApp()); | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(context) => const MaterialApp(home: VideoPlayerWidget()); | |
| } | |
| class VideoPlayerWidget extends StatefulWidget { | |
| final String url; | |
| final bool loop; | |
| final double volume; | |
| const VideoPlayerWidget({ | |
| super.key, | |
| this.url = 'https://commondatastorage.googleapis.com/' | |
| 'gtv-videos-bucket/sample/BigBuckBunny.mp4', | |
| this.loop = true, | |
| this.volume = 1, | |
| }); | |
| @override | |
| VideoPlayerWidgetState createState() => VideoPlayerWidgetState(); | |
| } | |
| class VideoPlayerWidgetState extends State<VideoPlayerWidget> { | |
| late VideoPlayerController _controller; | |
| late ValueNotifier<bool> _notifier; | |
| void _listener() => _notifier.value = _controller.value.isPlaying; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _controller = VideoPlayerController.networkUrl(Uri.parse(widget.url)) | |
| ..addListener(_listener); | |
| _notifier = ValueNotifier(false); | |
| } | |
| @override | |
| void dispose() { | |
| _controller | |
| ..removeListener(_listener) | |
| ..dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(context) => Scaffold( | |
| body: Center( | |
| child: FutureBuilder( | |
| future: Future.wait([ | |
| _controller.initialize(), | |
| _controller.setLooping(widget.loop), | |
| _controller.setVolume(widget.volume), | |
| ]), | |
| builder: (context, snapshot) { | |
| switch (snapshot.connectionState) { | |
| case ConnectionState.done: | |
| return snapshot.hasError | |
| ? Text('${snapshot.error}') | |
| : AspectRatio( | |
| aspectRatio: _controller.value.aspectRatio, | |
| child: VideoPlayer(_controller), | |
| ); | |
| default: | |
| return const CircularProgressIndicator(); | |
| } | |
| }, | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () => | |
| _notifier.value ? _controller.pause() : _controller.play(), | |
| child: ValueListenableBuilder( | |
| valueListenable: _notifier, | |
| builder: (context, isPlaying, _) => isPlaying | |
| ? const Icon(Icons.pause) | |
| : const Icon(Icons.play_arrow), | |
| ), | |
| ), | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment