Skip to content

Commit

Permalink
暂时解决屏幕自动旋转
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromexiong committed Mar 16, 2020
1 parent e3e8f48 commit b7f87ae
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 74 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
*.lock

build/

**/.classpath
**/.project
**/.settings
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## awsome_video_player
[![pub package](https://img.shields.io/pub/v/awsome_video_player.svg)](https://pub.dartlang.org/packages/awsome_video_player)

一个简单易用的而且可高度自定义的播放器。

![原型图](https://upload-images.jianshu.io/upload_images/4406914-032f148ae9fce8f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Expand Down
97 changes: 24 additions & 73 deletions lib/src/video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,8 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>
Animation<double> controlBottomBarAnimation;

/// 是否全屏
// bool fullscreened = false;
// bool fullscreened = false;

bool get fullscreened => _fullscreened;
bool _fullscreened = false;
get fullscreened {
return _fullscreened;
}

set fullscreened(bool isFull) {
_fullscreened = isFull;

Expand All @@ -105,7 +99,6 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>
Size get screenSize => MediaQuery.of(context).size;

StreamSubscription<ConnectivityResult> subscription;
StreamSubscription<DeviceOrientation> subOrientaion;

@override
void initState() {
Expand All @@ -132,21 +125,6 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>
}
});

/// 横竖屏监听
// subOrientaion = OrientationPlugin.onOrientationChange.listen((value) {
// if (!mounted) return;
// print(value);
// final full = value != DeviceOrientation.portraitUp;
// if (fullscreened == full) return;
// setState(() {
// fullscreened = full;
// });
// if (widget.onfullscreen != null) {
// widget.onfullscreen(fullscreened);
// }
// OrientationPlugin.forceOrientation(value);
// });

///运行设备横竖屏
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
Expand All @@ -170,7 +148,6 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>
]);
Screen.keepOn(false);
subscription.cancel();
subOrientaion.cancel();
super.dispose();
}

Expand Down Expand Up @@ -401,15 +378,7 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>
padding: EdgeInsets.symmetric(horizontal: 2),
child: GestureDetector(
onTap: () {
setState(() {
fullscreened = !fullscreened;
// if (widget.onfullscreen != null) {
// widget.onfullscreen(fullscreened);
// }
// OrientationPlugin.forceOrientation(fullscreened
// ? DeviceOrientation.landscapeRight
// : DeviceOrientation.portraitUp);
});
fullscreened = !fullscreened;
},
child: fullscreened
? widget.videoStyle.videoControlBarStyle.fullscreenExitIcon
Expand Down Expand Up @@ -438,15 +407,7 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>
videoControlBarStyle: widget.videoStyle.videoControlBarStyle,
onpop: () {
if (fullscreened) {
setState(() {
fullscreened = !fullscreened;
// if (widget.onfullscreen != null) {
// widget.onfullscreen(fullscreened);
// }
// OrientationPlugin.forceOrientation(fullscreened
// ? DeviceOrientation.landscapeRight
// : DeviceOrientation.portraitUp);
});
fullscreened = !fullscreened;
} else {
if (widget.onpop != null) {
widget.onpop(null);
Expand Down Expand Up @@ -517,16 +478,6 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>

@override
Widget build(BuildContext context) {
// setState(() {
// fullscreened = screenSize.width > screenSize.height;
// if (widget.onfullscreen != null) {
// widget.onfullscreen(fullscreened);
// }
// OrientationPlugin.forceOrientation(fullscreened
// ? DeviceOrientation.landscapeRight
// : DeviceOrientation.portraitUp);
// });

/// Loading...
if (!initialized)
return VideoLoadingView(
Expand Down Expand Up @@ -648,33 +599,33 @@ class _AwsomeVideoPlayerState extends State<AwsomeVideoPlayer>
videoChildrens.addAll(widget.children ?? []);

/// 构建video
return WillPopScope(
///监听返回按键
onWillPop: () {
if (fullscreened) {
setState(() {
fullscreened = !fullscreened;
// if (widget.onfullscreen != null) {
// widget.onfullscreen(fullscreened);
// }
// OrientationPlugin.forceOrientation(fullscreened
// ? DeviceOrientation.landscapeRight
// : DeviceOrientation.portraitUp);
});
return new Future.value(false);
} else {
return new Future.value(true);
}
},
child: AspectRatio(
return WillPopScope(onWillPop: () {
// 监听返回按键
if (fullscreened) {
fullscreened = !fullscreened;
return new Future.value(false);
} else {
return new Future.value(true);
}
}, child: OrientationBuilder(builder: (context, orientation) {
final full = orientation == Orientation.landscape;
if (_fullscreened != full) {
Future.delayed(Duration(milliseconds: 0)).then((e) {
_fullscreened = full;
if (widget.onfullscreen != null) {
widget.onfullscreen(fullscreened);
}
});
}
return AspectRatio(
aspectRatio: fullscreened
? _calculateAspectRatio(context)
: widget.playOptions.aspectRatio,

/// build 所有video组件
child: Stack(children: videoChildrens),
),
);
);
}));
}

/// 创建video controller
Expand Down
4 changes: 3 additions & 1 deletion lib/src/widget/video_top_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class VideoTopBar extends AnimatedWidget {
/// 返回按钮
GestureDetector(
onTap: () {
onpop != null && onpop();
if (onpop != null) {
onpop();
}
},
child: videoTopBarStyle.popIcon,
),
Expand Down

0 comments on commit b7f87ae

Please sign in to comment.