How to generate a Thumbnail from video path in flutter - flutter

I list all video files from internal storage now I want to show a thumbnail of every file in the leading property
i ask a question in flutter and hope will get a better response

For Others. Use video_player plugin as thumbnail. It's very effients comapare to those libraries and also works for ios.Just create statefullWidget as like item(if you want to show in list use that widget as item). See below example.
class _VideoItemState extends State<VideoItem> {
VideoPlayerController _controller;
#override
void initState() {
super.initState();
_controller = VideoPlayerController.network(widget.video.file.path)
..initialize().then((_) {
setState(() {}); //when your thumbnail will show.
});
}
#override
void dispose() {
super.dispose();
_controller.dispose();
}
#override
Widget build(BuildContext context) {
return ListTile(
leading: _controller.value.initialized
? Container(
width: 100.0,
height: 56.0,
child: VideoPlayer(_controller),
)
: CircularProgressIndicator(),
title: Text(widget.video.file.path.split('/').last),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
VideoPlayerPage(videoUrl: widget.video.file.path),
),
);
},
);
}
}

Related

It is possible to preserve state of PopUpMenuButton?

Currently i am working on music app and according to my ui i have to display download, downloading progress and downloaded status shown inside popup menu item.But according to Popup menu button widget behaviour, it is dispose and unmounted.So when i closed popup menu item and again open the last status always display download instead of downloading.So it is possible to prevent popup menu button after close.
I tried callback functions, provider, getx, auto keep alive and also stateful builder but it is not working.
I am using ValueNotifier to preserve the download progress. To preserve the state you can follow this structure and use state-management property like riverpod/bloc
class DTest extends StatefulWidget {
const DTest({super.key});
#override
State<DTest> createState() => _DTestState();
}
class _DTestState extends State<DTest> {
/// some state-management , also can be add a listener
ValueNotifier<double?> downloadProgress = ValueNotifier(null);
Timer? timer;
_startDownload() {
timer ??= Timer.periodic(
Duration(milliseconds: 10),
(timer) {
downloadProgress.value = (downloadProgress.value ?? 0) + .01;
if (downloadProgress.value! > 1) timer.cancel();
},
);
}
#override
void dispose() {
timer?.cancel();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
PopupMenuButton(
itemBuilder: (context) {
return [
PopupMenuItem(
child: ValueListenableBuilder(
valueListenable: downloadProgress,
builder: (context, value, child) => InkWell(
onTap: value == null ? _startDownload : null,
child: Text("${value ?? "Download"}")),
),
)
];
},
)
],
),
);
}
}

How do I control the quality of video uploaded using Flutter?

In Flutter I am working on a project to upload a high definition video and then user should be able to customise the video quality just like You tube. In Flutter I am using flick_video_player, a flutter library to play the videos.
Can anyone suggest a method to do the quality control, or any library packages that has quality control of video embedded in it?
class CustomVideoPlayer extends StatefulWidget
{
CustomVideoPlayer(this.item);
final item;
createState()
{
return _CustomvideoPlayerState(this.item);
}
}
class _CustomvideoPlayerState extends State<CustomVideoPlayer>
{
_CustomvideoPlayerState(this.item);
final item;
VideoPlayerController _videoPlayerController;
FlickManager flickManager;
var url;
#override initState()
{
super.initState();
setState(()
{
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
url = item["video_url"].toString();
SystemChrome.setEnabledSystemUIOverlays([]);
});
flickManager = FlickManager
(
videoPlayerController: VideoPlayerController.network(url),
);
}
#override
void dispose()
{
super.dispose();
_videoPlayerController.dispose();
flickManager.dispose();
}
Widget build(BuildContext context)
{
return WillPopScope
(
child: Scaffold
(
body: SafeArea
(
// top: true,
child: Column
(
children: <Widget>
[
Expanded
(
child: Center
(
child: FlickVideoPlayer
(
systemUIOverlay: [],
preferredDeviceOrientation: [DeviceOrientation.landscapeLeft],
flickManager: flickManager,
flickVideoWithControls: FlickVideoWithControls
(
controls: LandscapePlayerControls(item),
),
),
),
),
],
),
),
),
onWillPop: onWillPopS
);
}
// Handle back press
Future<bool> onWillPopS()
{
if(flickManager.flickVideoManager.isPlaying)
{
flickManager.flickControlManager.pause();
}
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
Navigator.pop(context);
return Future.value(true);
}
}
Above is the code and I am using custom controls to play, pause and skip the videos.
Can anybody please help me with this?

How do i play a network video in flutter

I am trying to put a network video from a different site in Flutter.
I have used the video_player package. I have used a future builder in which, the CircularProgressIndicator will keep running until the video is loaded. When i run the app, in the start the CircularProgressIndicator keep on running and after a few seconds it stops as if the video is loaded but it show complete blankness on the emulator. In other words the video is not loading.
After i start the app
After Loading
This is the code
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;
#override
void initState() {
_controller = VideoPlayerController.network(
'https://ok.ru/videoembed/1616636152346');
_initializeVideoPlayerFuture = _controller.initialize();
_controller.setLooping(true);
_controller.setVolume(1.0);
super.initState();
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
),
body: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.done){
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
);
} else {
return Center(
child: CircularProgressIndicator(backgroundColor: Colors.blue,
),
);
}
}
),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState(() {
if(_controller.value.isPlaying){
_controller.pause();
}else{
_controller.play();
}
});
},
child: Icon(_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
}
Your link for video is not a valid video link , it is a embed Url.
To Play a video from network URL use direct video URL like https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4.
Put this link in your videoController , it will working fine.

Flutter Splash Screen Video Player

I am trying to create a background video splash screen for my app.
Currently, I am achieving a blank screen by running this code.
void main() => runApp(WalkThrough());
class WalkThrough extends StatefulWidget {
#override
_WalkThroughState createState() => _WalkThroughState();
}
class _WalkThroughState extends State<WalkThrough> {
VideoPlayerController _controller;
#override
void initState() {
super.initState();
// Pointing the video controller to our local asset.
_controller = VideoPlayerController.asset('assets/video.mp4')
..initialize().then((_) {
// Once the video has been loaded we play the video and set looping to true.
_controller.play();
_controller.setLooping(true);
_controller.setVolume(0.0);
_controller.play();
// Ensure the first frame is shown after the video is initialized.
setState(() {});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
I suspect the problem may be here and have based my research off this Full screen video background in Flutter on Login as I am trying to achieve a similar result.
SizedBox.expand(
child: FittedBox(
// If your background video doesn't look right, try changing the BoxFit property.
// BoxFit.fill created the look I was going for.
fit: BoxFit.fill,
child: SizedBox(
width: _controller.value.size?.width ?? 0,
height: _controller.value.size?.height ?? 0,
child: VideoPlayer(_controller),
),
),
),
I think the video player package has an issue to show video in the Ios simulator. I had the same issue and search for it and find out this issue in Github. until now this issue is open. I tested the video player on a real device and there were no problems.
Use this video player video_player plugin
& Save your mp4 file in assets/videos location
Try the below code
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
import 'HomePage.dart';
class SplashPage extends StatefulWidget {
SplashPage({Key key}) : super(key: key);
#override
State<StatefulWidget> createState() => _SplashPageState();
}
class _SplashPageState extends State<SplashPage> {
VideoPlayerController _controller;
bool _visible = false;
#override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
_controller = VideoPlayerController.asset("assets/video/splash_video.mp4");
_controller.initialize().then((_) {
_controller.setLooping(true);
Timer(Duration(milliseconds: 100), () {
setState(() {
_controller.play();
_visible = true;
});
});
});
Future.delayed(Duration(seconds: 4), () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => HomePage(param_homepage: 'Welcome Home')),
(e) => false);
});
}
#override
void dispose() {
super.dispose();
if (_controller != null) {
_controller.dispose();
_controller = null;
}
}
_getVideoBackground() {
return AnimatedOpacity(
opacity: _visible ? 1.0 : 0.0,
duration: Duration(milliseconds: 1000),
child: VideoPlayer(_controller),
);
}
_getBackgroundColor() {
return Container(color: Colors.transparent //.withAlpha(120),
);
}
_getContent() {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
children: <Widget>[
_getVideoBackground(),
],
),
),
);
}
}

Flutter can not dispose VideoPlayerController

I am getting an exception thrown while using a back button after disposing of my VideoPlayerController.
I have my VideoPlayer and VideoPlayerController set up like this:
String url;
var plan;
_VideoPlayerScreenState(this.url,{this.plan});
Future<Null> OnWillPop(){
_controller.dispose();
if(plan!=null)
Navigator.push(context, MaterialPageRoute(builder: (context)=>
ListSession(plan :plan)));
else
Navigator.push(context, MaterialPageRoute(builder: (context)=>
ListMoves()));
}
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;
#override
void initState() {
_controller = VideoPlayerController.network(
url,
);
// Initialize the controller and store the Future for later use.
_initializeVideoPlayerFuture = _controller.initialize();
// Use the controller to loop the video.
_controller.setLooping(true);
_controller.play();
super.initState();
}
#override
void dispose() {
print("+++++++++++++++++++++++++++++++++++++++++++");
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return WillPopScope(onWillPop: OnWillPop,child:Scaffold(
// Use a FutureBuilder to display a loading spinner while waiting for the
// VideoPlayerController to finish initializing.
body: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return Center(
child: AspectRatio(
aspectRatio: _controller.value.aspectRatio,
// Use the VideoPlayer widget to display the video.
child: VideoPlayer(_controller),
),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return Center(child: CircularProgressIndicator());
}
},
),
));
}
but when I press the back button, I get this error:
Another exception was thrown: A VideoPlayerController was used after being disposed.
How do I properly dispose of my VideoPlayerController and be able to incorporate the back button?
You can copy paste run full code below
and mark unmark Navigator.push / Navigator.pushReplacement to check effect
In your case, you do not need to dispose of controller, because controller is local to this page, you can just do _controller.pause()
You use Navigator.push go to next page means you will pop from next page
If you dispose controller and set controller to null successfully, when pop from next page, you will get an error because initState will not be called again, controller will not init again
If you use Navigator.pushReplacement, dispose will be called automatically,
You can see console show _controller.dispose in demo code.
working demo
you can see when back from ListMove page, video still can play
full code
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() => runApp(VideoPlayerApp());
class VideoPlayerApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Video Player Demo',
home: VideoPlayerScreen(),
);
}
}
class VideoPlayerScreen extends StatefulWidget {
VideoPlayerScreen({Key key}) : super(key: key);
#override
_VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
VideoPlayerController _controller;
VideoPlayerController _oldController;
Future<void> _initializeVideoPlayerFuture;
#override
void initState() {
print("initState");
// Create and store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
);
// Initialize the controller and store the Future for later use.
_initializeVideoPlayerFuture = _controller.initialize();
// Use the controller to loop the video.
_controller.setLooping(true);
super.initState();
}
#override
void dispose() {
print("_controller.dispose");
// Ensure disposing of the VideoPlayerController to free up resources.
//_initializeVideoPlayerFuture = null;
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
await _controller.pause();
Navigator.push(
context, MaterialPageRoute(builder: (context) => ListMoves()));
/*Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => ListMoves()));*/
},
child: Scaffold(
appBar: AppBar(
title: Text('Butterfly Video'),
),
// Use a FutureBuilder to display a loading spinner while waiting for the
// VideoPlayerController to finish initializing.
body: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
// Use the VideoPlayer widget to display the video.
child: VideoPlayer(_controller),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Wrap the play or pause in a call to `setState`. This ensures the
// correct icon is shown.
setState(() {
// If the video is playing, pause it.
if (_controller.value.isPlaying) {
_controller.pause();
} else {
// If the video is paused, play it.
_controller.play();
}
});
},
// Display the correct icon depending on the state of the player.
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
), // This trailing comma makes auto-formatting nicer for build methods.
),
);
}
}
class ListMoves extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Movies'),
),
body: Center(
child: RaisedButton(
child: Text('Launch screen'),
onPressed: () {},
),
),
);
}
}