I don't want my video to look stretched. I'm using Chewie
flutter package as my video player.
I tried using _controller.value.size.aspectRatio
but it returns an error
The getter 'aspectRatio' was called on null.
here is my code to get a video from gallery:
Future uploadVideoFromGallery() async {
print("CALLED");
Map<PermissionGroup, PermissionStatus> permissions =
await PermissionHandler().requestPermissions(
[PermissionGroup.storage, PermissionGroup.camera]);
if (permissions[PermissionGroup.storage] == PermissionStatus.granted) {
var videoFile = await ImagePicker.pickVideo(source: ImageSource.gallery);
if (videoFile != null) {
getVideoThumbnail(videoFile.path);
setState(() {
isFileImage = false;
image = videoFile;
_controller = VideoPlayerController.file(image);
});
print(videoFile.path);
}
} else {
debugPrint('permission not granted');
}
}
and here is my code to place the videoFile
:
Chewie(
controller: ChewieController(
videoPlayerController: _controller,
aspectRatio: _controller.value.size.aspectRatio,
materialProgressColors: ChewieProgressColors(
playedColor: Color(colorSecondary),
handleColor: Color(colorPrimary),
bufferedColor: Color(colorPrimary),
),
placeholder: Container(
color: Colors.grey,
),
autoInitialize: true,
looping: false,
errorBuilder: (context, errorMessage) {
return Center(
child: Text(
errorMessage,
style: TextStyle(color: Color(colorText)),
),
);
},
),
)
You can calculate the aspect ratio of a video by dividing the video's width by its height. However, to calculate the video resolution, you'll multiply the video's width by its height.
To determine the aspect ratio of a screen: Measure the width and height of the screen. Divide the width by the height. Compare the result with the popular aspect ratios, e.g., 16:9 , to determine which standard your screen follows.
There is an issue on github regarding same.
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:chewie/chewie.dart';
import 'package:video_player/video_player.dart';
class MyVideoPlayer extends StatefulWidget {
final String path;
MyVideoPlayer({Key key, @required this.path}) : super(key: key);
@override
_MyVideoPlayerState createState() => new _MyVideoPlayerState();
}
class _MyVideoPlayerState extends State<MyVideoPlayer> {
VideoPlayerController _videoPlayerController;
ChewieController _chewieController;
Future<void> _future;
Future<void> initVideoPlayer() async {
await _videoPlayerController.initialize();
setState(() {
print(_videoPlayerController.value.aspectRatio);
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
aspectRatio: _videoPlayerController.value.aspectRatio,
autoPlay: false,
looping: false,
);
});
}
@override
void initState() {
super.initState();
// _controller = VideoPlayerController.network('https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4');
_videoPlayerController = VideoPlayerController.file(File(widget.path));
_future = initVideoPlayer();
}
@override
Widget build(BuildContext context) {
return new FutureBuilder(
future: _future,
builder: (context, snapshot) {
return new Center(
child: _videoPlayerController.value.initialized
? AspectRatio(
aspectRatio: _videoPlayerController.value.aspectRatio,
child: Chewie(
controller: _chewieController,
),
)
: new CircularProgressIndicator(),
);
}
);
}
@override
void dispose() {
_videoPlayerController.dispose();
_chewieController.dispose();
super.dispose();
}
}
This is the github url
If you're just using the regular video_player package, you can show an unstretched version of your video using the following code:
return FittedBox(
fit: BoxFit.cover,
child: SizedBox(
height: model.videoPlayerController.value.size?.height ?? 0,
width: model.videoPlayerController.value.size?.width ?? 0,
child: VideoPlayer(model.videoPlayerController),
),
);
I wrote an article on displaying videos from a URL in Flutter, too.
better_player plugin solution.
In initState:
betterPlayerController = BetterPlayerController(
betterPlayerConfiguration,
betterPlayerDataSource: betterPlayerDataSource,
);
betterPlayerController.addEventsListener((BetterPlayerEvent event) {
if (event.betterPlayerEventType == BetterPlayerEventType.initialized) {
betterPlayerController.setOverriddenAspectRatio(
betterPlayerController.videoPlayerController.value.aspectRatio);
setState(() {});
}
});
In body:
BetterPlayer(controller: betterPlayerController),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With