[英]Flutter - video_player fullscreen
I'm using a plugin called video_player on my Flutter project.我在我的 Flutter 项目中使用了一个名为video_player的插件。 I'm able to play and pause videos without a problem, but I want to make it fullscreen and horizontal.我可以毫无问题地播放和暂停视频,但我想让它全屏和水平播放。 I couldn't find anything related to this.我找不到与此相关的任何内容。
This is the basic code I'm using:这是我正在使用的基本代码:
playerController = VideoPlayerController.network(
"<VIDEO_URL>")
..addListener(listener)
..setVolume(1.0)
..initialize()
..play();
Can I make it fullscreen?我可以让它全屏吗?
As far as I understand, the VideoPlayer doesn't know anything about where it is, but rather just expands to fit within the given space the best it can.据我了解,VideoPlayer 对它所在的位置一无所知,而只是尽可能地扩展以适应给定的空间。
I believe what you want to do is use a RotatedBox
as a parent of the video and set the rotation value.我相信您想要做的是使用RotatedBox
作为视频的父级并设置旋转值。 Depending on how exactly your app works, you may want to have the video player start horizontal and small, and have a full screen button that switches to landscape mode.根据您的应用程序的具体工作方式,您可能希望视频播放器从水平和小尺寸开始,并有一个可切换到横向模式的全屏按钮。 However, if the app itself is set up to rotate you'll have to take that into account and un-rotate the video once the phone has been rotated horizontally, which will probably result in uglyness in the UI as the flutter rotation happens and you un-rotate the video.但是,如果应用程序本身设置为旋转,则您必须考虑到这一点,并在手机水平旋转后取消旋转视频,这可能会在发生颤动旋转时导致 UI 丑陋,而您取消旋转视频。
You probably also want to use a dialog to show the video full-screen so that you can dismiss it and get back to the screen you were at.您可能还想使用一个对话框来全屏显示视频,以便您可以关闭它并返回到您所在的屏幕。
I could probably provide a bit more guidance but it does depend on which way you go with that (either locking the app to portrait mode and doing the rotation manually) vs using flutter's built-in rotation.我可能会提供更多指导,但这取决于您采用哪种方式(将应用程序锁定为纵向模式并手动进行旋转)与使用 flutter 的内置旋转。
I have another situation for this question, that's use Chewie plugin, you can install it right here: https://pub.dev/packages/chewie And this is the code that I implemented it:我对这个问题有另一种情况,那就是使用Chewie插件,你可以在这里安装它: https : //pub.dev/packages/chewie这是我实现它的代码:
VideoPlayerController _videoPlayerController;
ChewieController _chewieController;
double _aspectRatio = 16 / 9;
@override
initState() {
super.initState();
print(widget.videoUrl);
_videoPlayerController = VideoPlayerController.network(widget.videoUrl);
_chewieController = ChewieController(
allowedScreenSleep: false,
allowFullScreen: true,
deviceOrientationsAfterFullScreen: [
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
],
videoPlayerController: _videoPlayerController,
aspectRatio: _aspectRatio,
autoInitialize: true,
autoPlay: true,
showControls: true,
);
_chewieController.addListener(() {
if (_chewieController.isFullScreen) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
} else {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
});
}
Remember restore orientation of the device after exit page:记住退出页面后恢复设备的方向:
@override
void dispose() {
_videoPlayerController.dispose();
_chewieController.dispose();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
child: Chewie(
controller: _chewieController,
),
),
),
);
}
In order for it to work in iOS you should add the following change in ios/Runner/Info.plist
:为了让它在 iOS 中工作,你应该在ios/Runner/Info.plist
添加以下更改:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
I have created a function which will play video in fullscreen我创建了一个可以全屏播放视频的功能
import 'package:flutter/services.dart';
.
.
void pushFullScreenVideo() {
//This will help to hide the status bar and bottom bar of Mobile
//also helps you to set preferred device orientations like landscape
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
SystemChrome.setEnabledSystemUIOverlays([]);
SystemChrome.setPreferredOrientations(
[
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
],
);
//This will help you to push fullscreen view of video player on top of current page
Navigator.of(navigatorKey.currentContext)
.push(
PageRouteBuilder(
opaque: false,
settings: RouteSettings(),
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return Scaffold(
backgroundColor: Colors.transparent,
resizeToAvoidBottomPadding: false,
body: Dismissible(
key: const Key('key'),
direction: DismissDirection.vertical,
onDismissed: (_) => Navigator.of(context).pop(),
child: OrientationBuilder(
builder: (context, orientation) {
isPortrait = orientation == Orientation.portrait;
return Center(
child: Stack(
//This will help to expand video in Horizontal mode till last pixel of screen
fit: isPortrait ? StackFit.loose : StackFit.expand,
children: [
AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: VideoPlayer(controller),
),
],
),
);
},
);
},
),
)
.then(
(value) {
//This will help you to set previous Device orientations of screen so App will continue for portrait mode
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
SystemChrome.setPreferredOrientations(
[
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
],
);
},
);
}
Even tho this is a late answer figured it might help someone,即使这是一个迟到的答案,我认为它可能对某人有帮助,
I have found flick_video_player to be one of the best video players out there.我发现flick_video_player是最好的视频播放器之一。
You can simply enter to full screen using the predefined method, using the controller.您可以使用预定义的方法简单地进入全屏,使用 controller。
_activeManager!.flickControlManager!.enterFullscreen();
and to exit from the fullscreen player并退出全屏播放器
_activeManager!.flickControlManager!.exitFullscreen();
You can get examples here你可以在这里得到例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.