You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Flutter Custom Video Player Doesn't respect Parent Container Width and Height #48854
I have added a VideoPlayer and added it as a child of a Container Widget. Now when video loads, that video player sizes itself based on the video file aspect ratio and doesn't stay within parent container.
I tried with SizedBox and FittedBox widgets too but same issue
VideoPlayerController _controller;
Future _initializeVideoPlayerFuture;
bool _volumeUp = true;
var _scaffoldKey = new GlobalKey();
var a;
@OverRide
void initState() {
// Create and store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.network(
widget.videoUrl
);
// Initialize the controller and store the Future for later use.
_initializeVideoPlayerFuture = _controller.initialize();
_controller.addListener(checkIfVideoFinished);
super.initState();
@OverRide
void dispose() {
// Ensure disposing of the VideoPlayerController to free up resources.
_controller.removeListener(checkIfVideoFinished);
_controller.dispose();
super.dispose();
}
@OverRide
Widget build(BuildContext context) {
return VisibilityDetector(
key: _scaffoldKey,
onVisibilityChanged: (VisibilityInfo info) {
if(info.visibleFraction < 1.0) {
if(_controller.value.isPlaying) {
if(this.mounted) {
setState(() {
_controller.pause();
});
}
}
}
// debugPrint( widget.videoUrl + "\n ${info.visibleFraction} of my widget is visible");
},
child: Stack(
children: [
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 Align(
alignment: Alignment.center,
child: AspectRatio(
aspectRatio: 4/3,
child: VideoPlayer(_controller),
),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return Center(child: CircularProgressIndicator());
}
},
),
Align(
alignment: Alignment.bottomRight,
child: Container(
color: Colors.black87.withOpacity(0.3),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,),
color: Colors.white,
onPressed: () {
if (_controller.value.isPlaying) {
setState(() {
_controller.pause();
});
_controller.removeListener(checkIfVideoFinished);
} else {
setState(() {
_controller.play();
});
_controller.addListener(checkIfVideoFinished);
}
},
),
IconButton(
icon: Icon(_volumeUp ? Icons.volume_up : Icons.volume_off,),
color: Colors.white,
onPressed: () {
setState(() {
if (_volumeUp) {
_controller.setVolume(0.0);
_volumeUp = false;
} else {
_controller.setVolume(1.0);
_volumeUp = true;
}
});
},
),
],
),
),
),
],
),
);
}
}
Hi @n-santamaria
could you (or anyone with the same problem)
please file a new issue with the exact descriptions what happens, logs and the output of 'flutter doctor -v' please.
All system setups can be slightly different so it's always better to open new issues and reference related issues.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
iapicca commentedon Jan 15, 2020
Hi @premingiet
I see here that you are using
the third party plugin flutter_ijkplayer,
you may want to open an issue in the dedicated github
Closing, as this isn't an issue with Flutter itself,
if you disagree please write in the comments and I will reopen it
Thank you
n-santamaria commentedon Feb 21, 2020
Hello @iapicca,
I've faced the similar issue with video_player plugin.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_widgets/flutter_widgets.dart';
import 'package:video_player/video_player.dart';
class VideoPlayerScreen extends StatefulWidget {
final String videoUrl;
VideoPlayerScreen({@required this.videoUrl, Key key}) : super(key: key);
@OverRide
_VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}
class _VideoPlayerScreenState extends State {
VideoPlayerController _controller;
Future _initializeVideoPlayerFuture;
bool _volumeUp = true;
var _scaffoldKey = new GlobalKey();
var a;
@OverRide
void initState() {
// Create and store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.network(
widget.videoUrl
);
}
void checkIfVideoFinished() {
if(_controller.value.position == _controller.value.duration) {
_controller.removeListener(checkIfVideoFinished);
setState(() {
_controller.pause();
});
}
}
@OverRide
void dispose() {
// Ensure disposing of the VideoPlayerController to free up resources.
_controller.removeListener(checkIfVideoFinished);
_controller.dispose();
}
@OverRide
Widget build(BuildContext context) {
return VisibilityDetector(
key: _scaffoldKey,
onVisibilityChanged: (VisibilityInfo info) {
if(info.visibleFraction < 1.0) {
if(_controller.value.isPlaying) {
if(this.mounted) {
setState(() {
_controller.pause();
});
}
}
}
// debugPrint( widget.videoUrl + "\n ${info.visibleFraction} of my widget is visible");
},
child: Stack(
children: [
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 Align(
alignment: Alignment.center,
child: AspectRatio(
aspectRatio: 4/3,
child: VideoPlayer(_controller),
),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return Center(child: CircularProgressIndicator());
}
},
),
Align(
alignment: Alignment.bottomRight,
child: Container(
color: Colors.black87.withOpacity(0.3),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,),
color: Colors.white,
onPressed: () {
if (_controller.value.isPlaying) {
setState(() {
_controller.pause();
});
_controller.removeListener(checkIfVideoFinished);
} else {
setState(() {
_controller.play();
});
_controller.addListener(checkIfVideoFinished);
}
},
),
IconButton(
icon: Icon(_volumeUp ? Icons.volume_up : Icons.volume_off,),
color: Colors.white,
onPressed: () {
setState(() {
if (_volumeUp) {
_controller.setVolume(0.0);
_volumeUp = false;
} else {
_controller.setVolume(1.0);
_volumeUp = true;
}
});
},
),
],
),
),
),
],
),
);
}
}
iapicca commentedon Feb 21, 2020
Hi @n-santamaria
could you (or anyone with the same problem)
please file a new issue with the exact descriptions what happens, logs and the output of 'flutter doctor -v' please.
All system setups can be slightly different so it's always better to open new issues and reference related issues.
n-santamaria commentedon Feb 22, 2020
Hello @iapicca
I've created a new issue.
Best Regards,
Neville Santamaria
github-actions commentedon Aug 22, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.