【发布时间】:2020-12-26 10:02:25
【问题描述】:
我已通过添加将我的应用屏幕方向设置为纵向
android:screenOrientation="portrait"
到我的 android/app/src/main/AndroidManifest.xml ,所以当我启动我的应用程序时,它被锁定为纵向模式。我很高兴。
我有一个显示视频的页面,我正在使用 Chewie 插件,它可以在横向模式下播放视频。
这是我的代码:
class _VideoState extends State<Video> {
ChewieController _chewieController;
@override
void initState() {
super.initState();
_chewieController = ChewieController(
fullScreenByDefault: true,
videoPlayerController: widget.videoPlayerController,
aspectRatio: 16 / 9,
autoInitialize: true,
autoPlay: true,
allowedScreenSleep: false,
allowFullScreen: true,
deviceOrientationsAfterFullScreen: [
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
],
errorBuilder: (context, e) {
return Center(
child: Text('Some error occurred'),
);
},
);
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Chewie(
controller: _chewieController,
),
);
}
@override
void dispose() {
widget.videoPlayerController.dispose();
widget.videoPlayerController.pause();
_chewieController.dispose();
super.dispose();
}
}
这工作正常,直到我完全以横向运行视频,没有任何问题,但是当我回到我的上一页时,我应该处于纵向模式的应用程序现在处于横向模式,我不希望发生这种情况。我希望我的应用仍被锁定为纵向模式。
我确实尝试将 SystemChrome.setPreferredOrientations 添加到我的视频页面并丢弃,但这也不起作用。
感谢任何帮助。
这是我得到的错误:
══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for VideoPlayerController:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4182 pos 12:
'_debugLifecycleState != _ElementLifecycle.defunct': is not true.
Either the assertion indicates an error in the framework itself, or we should provide substantially
more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
When the exception was thrown, this was the stack:
#2 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4182:12)
#3 State.setState (package:flutter/src/widgets/framework.dart:1260:14)
#4 _VideoState.initState.<anonymous closure> (package:/widget/page/video.dart:65:9)
#5 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:209:21)
#6 ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:276:5)
#7 VideoPlayerController.pause (package:video_player/video_player.dart:360:5)
#8 _VideoState.dispose (package:/widget/page/video.dart:107:34)
#9 StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4773:12)
(elided 5 frames from class _AssertionError and dart:async)
The VideoPlayerController sending notification was:
VideoPlayerController#2afe2(VideoPlayerValue(duration: 0:10:55.430000, size: Size(1920.0, 1080.0),
position: 0:10:55.430000, caption: Instance of 'Caption', buffered: [DurationRange(start:
0:00:00.000000, end: 0:10:55.430000)], isPlaying: false, isLooping: false, isBuffering:
falsevolume: 1.0, errorDescription: null))
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by foundation library ════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for VideoPlayerController:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4182 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true.
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
When the exception was thrown, this was the stack:
#2 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4182:12)
#3 State.setState (package:flutter/src/widgets/framework.dart:1260:14)
#4 _VideoState.initState.<anonymous closure> (package:/widget/page/video.dart:65:9)
#5 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:209:21)
#6 ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:276:5)
...
The VideoPlayerController sending notification was: VideoPlayerController#2afe2(VideoPlayerValue(duration: 0:10:55.430000, size: Size(1920.0, 1080.0), position: 0:10:55.430000, caption: Instance of 'Caption', buffered: [DurationRange(start: 0:00:00.000000, end: 0:10:55.430000)], isPlaying: false, isLooping: false, isBuffering: falsevolume: 1.0, errorDescription: null))
这是我的更新代码:
class Video extends StatefulWidget {
final VideoPlayerController videoPlayerController;
const Video({
Key key,
@required this.videoPlayerController,
}) : super(key: key);
@override
_VideoState createState() => _VideoState();
}
class _VideoState extends State<Video> {
ChewieController _chewieController;
@override
void initState() {
super.initState();
_chewieController = ChewieController(
fullScreenByDefault: true,
videoPlayerController: widget.videoPlayerController,
aspectRatio: 16 / 9,
autoInitialize: true,
autoPlay: true,
allowedScreenSleep: false,
allowFullScreen: true,
deviceOrientationsAfterFullScreen: [
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
],
errorBuilder: (context, e) {
return Center(
child: Text('Some error occurred'),
);
},
);
}
@override
Widget build(BuildContext context) {
//final length = MediaQuery.of(context).size;
return SingleChildScrollView(
child: VideoScaffold(
Chewie(
controller: _chewieController,
),
),
);
}
@override
void dispose() {
widget.videoPlayerController.dispose();
widget.videoPlayerController.pause();
_chewieController.dispose();
super.dispose();
}
}
class VideoScaffold extends StatefulWidget {
const VideoScaffold({Key key, this.child}) : super(key: key);
final Widget child;
@override
State<StatefulWidget> createState() => _VideoScaffoldState();
}
class _VideoScaffoldState extends State<VideoScaffold> {
@override
void initState() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
AutoOrientation.landscapeAutoMode();
super.initState();
}
@override
Widget build(BuildContext context) {
return widget.child;
}
@override
dispose() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
AutoOrientation.landscapeAutoMode();
AutoOrientation.portraitUpMode();
super.dispose();
}
}
现在从视频页面执行 Navigator.of(context).pop 后,我的应用程序保持纵向模式,但现在问题是我收到上述错误。谁能帮我摆脱这个错误?
【问题讨论】:
-
您是否尝试将
android:screenOrientation="portrait"直接添加到您希望仅在清单中显示为纵向的活动? -
我已将 android:screenOrientation="portrait" 添加到我的 android/app/src/main/AndroidManifest.xml
-
对,您的清单中是否定义了多个活动?我不熟悉 Chewy 插件,但大概它会启动自己的活动?您可以在活动级别指定
screenOrientation。所以在<activity android:name="portraitActivity">中定义screenOrientation -
不走运,问题还是一样。