383462ef
吴启风
feat: 新增视频专区列表与播放功能
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:wow_english/common/request/dao/video_dao.dart';
import 'video_zone_item.dart';
class VideoZonePlayerPage extends StatefulWidget {
const VideoZonePlayerPage({
super.key,
required this.items,
this.initialVideoId,
}) : assert(items.length > 0, '视频播放列表不能为空');
final List<VideoZoneItem> items;
final int? initialVideoId;
@override
State<VideoZonePlayerPage> createState() => _VideoZonePlayerPageState();
}
class _VideoZonePlayerPageState extends State<VideoZonePlayerPage>
with WidgetsBindingObserver {
VideoPlayerController? _controller;
Timer? _progressTimer;
late int _currentIndex;
double _playbackSpeed = 1;
bool _showControls = true;
bool _switchingVideo = false;
bool _handlingCompletion = false;
int? _lastReportedPosition;
int? _completedReportedVideoId;
Object? _loadError;
VideoZoneItem get _currentItem => widget.items[_currentIndex];
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
final index = widget.initialVideoId == null
? 0
: widget.items.indexWhere((item) => item.id == widget.initialVideoId);
_currentIndex = index < 0 ? 0 : index;
_loadCurrentVideo();
}
Future<void> _loadCurrentVideo() async {
_progressTimer?.cancel();
final oldController = _controller;
oldController?.removeListener(_onVideoChanged);
final controller =
VideoPlayerController.networkUrl(Uri.parse(_currentItem.videoUrl));
_controller = controller;
_loadError = null;
if (mounted) {
setState(() {});
}
await oldController?.dispose();
try {
await controller.initialize();
if (!mounted || controller != _controller) {
await controller.dispose();
return;
}
await controller.setLooping(false);
await controller.setPlaybackSpeed(_playbackSpeed);
final savedProgress =
_currentItem.completed ? 0 : _currentItem.progressSeconds;
if (savedProgress > 0 &&
savedProgress < controller.value.duration.inSeconds) {
await controller.seekTo(Duration(seconds: savedProgress));
}
controller.addListener(_onVideoChanged);
await controller.play();
_lastReportedPosition = null;
_handlingCompletion = false;
_progressTimer = Timer.periodic(
const Duration(seconds: 5),
(_) => _reportCurrentProgress(),
);
setState(() {});
} catch (error) {
if (mounted && controller == _controller) {
setState(() => _loadError = error);
}
}
}
void _onVideoChanged() {
final controller = _controller;
if (!mounted || controller == null) return;
if (controller.value.isCompleted &&
!_switchingVideo &&
!_handlingCompletion &&
_completedReportedVideoId != _currentItem.id) {
_handlingCompletion = true;
unawaited(_handleVideoCompleted());
return;
}
setState(() {});
}
Future<void> _playNext({bool currentCompleted = false}) async {
if (_switchingVideo || _currentIndex >= widget.items.length - 1) return;
_switchingVideo = true;
await _reportCurrentProgress(completed: currentCompleted, force: true);
setState(() => _currentIndex++);
await _loadCurrentVideo();
_switchingVideo = false;
}
Future<void> _handleVideoCompleted() async {
if (_currentIndex < widget.items.length - 1) {
await _playNext(currentCompleted: true);
} else {
await _reportCurrentProgress(completed: true, force: true);
}
_handlingCompletion = false;
}
Future<void> _togglePlay() async {
final controller = _controller;
if (controller == null || !controller.value.isInitialized) return;
if (controller.value.isCompleted) {
await controller.seekTo(Duration.zero);
_completedReportedVideoId = null;
}
if (controller.value.isPlaying) {
await controller.pause();
await _reportCurrentProgress(force: true);
} else {
await controller.play();
}
if (mounted) setState(() {});
}
Future<void> _setSpeed(double speed) async {
_playbackSpeed = speed;
await _controller?.setPlaybackSpeed(speed);
if (mounted) setState(() {});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_progressTimer?.cancel();
_controller?.removeListener(_onVideoChanged);
unawaited(_reportCurrentProgress(force: true));
_controller?.dispose();
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.inactive ||
state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
unawaited(_reportCurrentProgress(force: true));
}
}
Future<void> _reportCurrentProgress({
bool? completed,
bool force = false,
}) async {
final controller = _controller;
if (controller == null || !controller.value.isInitialized) return;
final durationSeconds = controller.value.duration.inSeconds > 0
? controller.value.duration.inSeconds
: _currentItem.durationSeconds;
final progressSeconds = controller.value.position.inSeconds
.clamp(0, durationSeconds > 0 ? durationSeconds : 0);
final isCompleted = completed ?? controller.value.isCompleted;
if (!force && _lastReportedPosition == progressSeconds) return;
if (isCompleted && _completedReportedVideoId == _currentItem.id) return;
_lastReportedPosition = progressSeconds;
try {
await VideoDao.savePlayRecord(
videoId: _currentItem.id,
progressSeconds: progressSeconds,
durationSeconds: durationSeconds,
completed: isCompleted,
);
if (isCompleted) _completedReportedVideoId = _currentItem.id;
} catch (error) {
debugPrint('保存视频播放记录失败: $error');
}
}
@override
Widget build(BuildContext context) {
final controller = _controller;
return Scaffold(
backgroundColor: Colors.black,
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => setState(() => _showControls = !_showControls),
onDoubleTap: _togglePlay,
child: Stack(
fit: StackFit.expand,
children: [
if (controller?.value.isInitialized == true)
Center(
child: AspectRatio(
aspectRatio: controller!.value.aspectRatio,
child: VideoPlayer(controller),
),
)
else if (_loadError != null)
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('视频加载失败',
style: TextStyle(color: Colors.white, fontSize: 18)),
const SizedBox(height: 12),
FilledButton(
onPressed: _loadCurrentVideo, child: const Text('重试')),
],
),
)
else
const Center(child: CircularProgressIndicator()),
if (_showControls) _buildControls(controller),
],
),
),
);
}
Widget _buildControls(VideoPlayerController? controller) {
final initialized = controller?.value.isInitialized == true;
return ColoredBox(
color: Colors.black38,
child: SafeArea(
child: Column(
children: [
Row(
children: [
IconButton(
onPressed: () => Navigator.of(context).pop(),
icon:
const Icon(Icons.arrow_back_ios_new, color: Colors.white),
),
Expanded(
child: Text(
_currentItem.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(color: Colors.white, fontSize: 20),
),
),
PopupMenuButton<double>(
initialValue: _playbackSpeed,
onSelected: _setSpeed,
itemBuilder: (_) => const [0.5, 0.75, 1.0, 1.25, 1.5, 2.0]
.map((speed) => PopupMenuItem(
value: speed,
child: Text('${speed}x'),
))
.toList(),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 18, vertical: 12),
child: Text('${_playbackSpeed}x',
style:
const TextStyle(color: Colors.white, fontSize: 16)),
),
),
],
),
const Spacer(),
if (initialized)
IconButton(
onPressed: _togglePlay,
iconSize: 68,
color: Colors.white,
icon: Icon(controller!.value.isPlaying
? Icons.pause_circle_filled
: Icons.play_circle_fill),
),
const Spacer(),
if (initialized)
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 12),
child: Row(
children: [
Expanded(
child: VideoProgressIndicator(
controller!,
allowScrubbing: true,
padding: const EdgeInsets.symmetric(vertical: 12),
colors: const VideoProgressColors(
playedColor: Color(0xFF00B6F1),
bufferedColor: Colors.white54,
backgroundColor: Colors.white24,
),
),
),
const SizedBox(width: 12),
Text(
'${_formatDuration(controller.value.position)} / '
'${_formatDuration(controller.value.duration)}',
style: const TextStyle(color: Colors.white),
),
if (_currentIndex < widget.items.length - 1) ...[
const SizedBox(width: 8),
IconButton(
tooltip: '下一集',
onPressed: _playNext,
icon: const Icon(Icons.skip_next, color: Colors.white),
),
],
],
),
),
],
),
),
);
}
String _formatDuration(Duration duration) {
final minutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
final seconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
if (duration.inHours == 0) return '$minutes:$seconds';
return '${duration.inHours.toString().padLeft(2, '0')}:$minutes:$seconds';
}
}
|