Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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