class addVideo extends StatefulWidget { const addVideo({Key? key}) : super(key: key); @override _addVideoState createState() => _addVideoState(); } class _addVideoState extends State<addVideo> { bool uploading = false; double val = 0; CollectionReference? vidRef; firebase_storage.Reference? ref; File? video; VideoPlayerController? videoPlayerController; final picker = ImagePicker(); Future pickVideo(ImageSource source) async { final Video = await picker.pickVideo(source: source); video = File(Video!.path); videoPlayerController = VideoPlayerController.file(video!) ..initialize().then((_) { setState(() {}); videoPlayerController!.play(); }); } List<File> _video = []; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Add video'), actions: [ ElevatedButton( onPressed: () { setState(() { uploading = true; }); uploadFile().whenComplete(() => Navigator.of(context).pop()); }, child: Text( 'Upload Video', style: TextStyle(color: Colors.red), ), ) ], ), body: ListView.builder( itemCount: _video.length + 1, itemBuilder: (ctx, index) { return index == 0 ? Center( child: ElevatedButton( onPressed: () { !uploading ? chooseVideo() : null; }, child: const Text( 'Test', style: TextStyle(color: Colors.black), )), ) : AspectRatio( aspectRatio: videoPlayerController!.value.aspectRatio, // child: VideoPlayer(videoPlayerController!), child: VideoPlayer( VideoPlayerController.file(_video[index - 1]) ..initialize().then((_) { setState(() {}); videoPlayerController!.play(); })), ); }), ); } chooseVideo() async { ImagePicker picker = ImagePicker(); final pickedFile = await picker.pickVideo(source: ImageSource.gallery); setState(() { _video.add(File(pickedFile!.path)); }); videoPlayerController = VideoPlayerController.file(File(pickedFile!.path)) ..initialize().then((_) { setState(() {}); videoPlayerController!.play(); }); if (pickedFile.path == null) retrieveLostData(); } Future<void> retrieveLostData() async { final LostDataResponse = await ImagePicker.platform.getLostData(); if (LostDataResponse.isEmpty) { return; } if (LostDataResponse != null) { setState(() { _video.add(File(LostDataResponse.file!.path)); }); } else { print(LostDataResponse.file); } } Future uploadFile() async { int i = 1; for (var vid in _video) { setState(() { val = i / _video.length; }); ref = firebase_storage.FirebaseStorage.instance .ref() .child('videos/${Path.basename(vid.path)}'); await ref!.putFile(vid).whenComplete(() async { await ref!.getDownloadURL().then((value) { vidRef!.add({ 'url': value }); i++; }); }); } } @override void initState() { super.initState(); vidRef = FirebaseFirestore.instance.collection( 'Videourls'); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)