Skip to content

Instantly share code, notes, and snippets.

@nikhilmufc7
Created September 21, 2021 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhilmufc7/275e3e1bb887e59ec026388dcf4a2d06 to your computer and use it in GitHub Desktop.
Save nikhilmufc7/275e3e1bb887e59ec026388dcf4a2d06 to your computer and use it in GitHub Desktop.
Video Player Flutter Demo with Controls
import 'dart:io';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Video Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var pickedFile;
var _controller;
var chewieController;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
MaterialButton(
onPressed: () async {
pickedFile = await ImagePicker.platform
.pickVideo(source: ImageSource.camera);
if (pickedFile != null) {
setState(() {
_controller = VideoPlayerController.file(
File(pickedFile.path),
)..initialize().then((_) {
chewieController = ChewieController(
videoPlayerController: _controller,
autoPlay: true,
looping: true,
);
setState(() {});
});
});
}
},
child: Text("Pick Video"),
),
Container(
height: 300,
width: 300,
child: Center(
child:
chewieController != null && _controller.value.isInitialized
? Chewie(
controller: chewieController,
)
: Container(),
),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment