Skip to content

Commit f69f769

Browse files
committed
🐛 Handle iCloud videos more gracefully
Fix #197
1 parent 24cb700 commit f69f769

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

lib/src/widget/builder/video_page_builder.dart

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class VideoPageBuilder extends StatefulWidget {
3434
class _VideoPageBuilderState extends State<VideoPageBuilder> {
3535
/// Controller for the video player.
3636
/// 视频播放的控制器
37-
late final VideoPlayerController _controller;
37+
VideoPlayerController get controller => _controller!;
38+
VideoPlayerController? _controller;
3839

3940
/// Whether the controller has initialized.
4041
/// 控制器是否已初始化
@@ -50,27 +51,27 @@ class _VideoPageBuilderState extends State<VideoPageBuilder> {
5051

5152
/// Whether the controller is playing.
5253
/// 播放控制器是否在播放
53-
bool get isControllerPlaying => _controller.value.isPlaying;
54+
bool get isControllerPlaying => _controller?.value.isPlaying ?? false;
5455

55-
@override
56-
void initState() {
57-
super.initState();
58-
initializeVideoPlayerController();
59-
}
56+
bool _isInitializing = false;
57+
bool _isLocallyAvailable = false;
6058

6159
@override
6260
void dispose() {
6361
/// Remove listener from the controller and dispose it when widget dispose.
6462
/// 部件销毁时移除控制器的监听并销毁控制器。
65-
_controller.removeListener(videoPlayerListener);
66-
_controller.pause();
67-
_controller.dispose();
63+
_controller
64+
?..removeListener(videoPlayerListener)
65+
..pause()
66+
..dispose();
6867
super.dispose();
6968
}
7069

7170
/// Get media url from the asset, then initialize the controller and add with a listener.
7271
/// 从资源获取媒体url后初始化,并添加监听。
7372
Future<void> initializeVideoPlayerController() async {
73+
_isInitializing = true;
74+
_isLocallyAvailable = true;
7475
final String? url = await widget.asset.getMediaUrl();
7576
if (url == null) {
7677
hasErrorWhenInitializing = true;
@@ -81,13 +82,13 @@ class _VideoPageBuilderState extends State<VideoPageBuilder> {
8182
}
8283
_controller = VideoPlayerController.network(Uri.parse(url).toString());
8384
try {
84-
await _controller.initialize();
85+
await controller.initialize();
8586
hasLoaded = true;
86-
_controller
87+
controller
8788
..addListener(videoPlayerListener)
8889
..setLooping(widget.hasOnlyOneVideoAndMoment);
8990
if (widget.hasOnlyOneVideoAndMoment) {
90-
_controller.play();
91+
controller.play();
9192
}
9293
} catch (e) {
9394
realDebugPrint('Error when initialize video controller: $e');
@@ -115,19 +116,19 @@ class _VideoPageBuilderState extends State<VideoPageBuilder> {
115116
/// 一般来说按钮只切换播放暂停。当视频播放结束时,点击按钮将从头开始播放。
116117
Future<void> playButtonCallback() async {
117118
if (isPlaying.value) {
118-
_controller.pause();
119-
} else {
120-
if (widget.delegate.isDisplayingDetail.value) {
121-
widget.delegate.switchDisplayingDetail(value: false);
122-
}
123-
if (_controller.value.duration == _controller.value.position) {
124-
_controller
125-
..seekTo(Duration.zero)
126-
..play();
127-
} else {
128-
_controller.play();
129-
}
119+
controller.pause();
120+
return;
130121
}
122+
if (widget.delegate.isDisplayingDetail.value) {
123+
widget.delegate.switchDisplayingDetail(value: false);
124+
}
125+
if (controller.value.duration == controller.value.position) {
126+
controller
127+
..seekTo(Duration.zero)
128+
..play();
129+
return;
130+
}
131+
controller.play();
131132
}
132133

133134
@override
@@ -138,6 +139,9 @@ class _VideoPageBuilderState extends State<VideoPageBuilder> {
138139
if (hasErrorWhenInitializing) {
139140
return Center(child: ScaleText(Constants.textDelegate.loadFailed));
140141
}
142+
if (!_isLocallyAvailable && !_isInitializing) {
143+
initializeVideoPlayerController();
144+
}
141145
if (!hasLoaded) {
142146
return const SizedBox.shrink();
143147
}
@@ -147,8 +151,8 @@ class _VideoPageBuilderState extends State<VideoPageBuilder> {
147151
Positioned.fill(
148152
child: Center(
149153
child: AspectRatio(
150-
aspectRatio: _controller.value.aspectRatio,
151-
child: VideoPlayer(_controller),
154+
aspectRatio: controller.value.aspectRatio,
155+
child: VideoPlayer(controller),
152156
),
153157
),
154158
),

0 commit comments

Comments
 (0)