import 'dart:io';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:image_picker/image_picker.dart';
class VideoWatch extends StatefulWidget {
@override
_VideoWatchState createState() => _VideoWatchState();
}
class _VideoWatchState extends State<VideoWatch> {
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;
@override
void initState() {
super.initState();
_controller = VideoPlayerController.network(
'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4');
_initializeVideoPlayerFuture = _controller.initialize();
_controller.setLooping(true);
_controller.play();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _loadVideo() async {
File videoFile = await ImagePicker.pickVideo(source: ImageSource.gallery);
if (videoFile != null) {
setState(() {
_controller = VideoPlayerController.file(videoFile);
_initializeVideoPlayerFuture = _controller.initialize();
_controller.setLooping(true);
_controller.play();
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Watch Video'),
),
body: Container(
alignment: Alignment.center,
child: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
);
} else {
return CircularProgressIndicator();
}
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _loadVideo,
child: Icon(Icons.video_library),
),
);
}
}
Explanations: This code creates a Flutter app that allows users to watch videos from their device's gallery. The VideoPlayer widget is used to display the video, while the VideoPlayerController class is used to control the video playback. The ImagePicker library is used to allow users to select a video from their device's gallery. When the user presses the floating action button, the _loadVideo function is called, which opens the device's gallery and allows the user to select a video. The selected video is then set as the source for the VideoPlayerController and played in the app.