Skip to content

Commit e9291af

Browse files
authored
Pass the Player directly to the Control widget (#130)
1 parent 440c53a commit e9291af

File tree

2 files changed

+51
-60
lines changed

2 files changed

+51
-60
lines changed

lib/src/widgets/controls.dart

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:dart_vlc_ffi/src/playerState/playerState.dart';
88

99
class Control extends StatefulWidget {
1010
final Widget child;
11-
final int playerId;
11+
final Player player;
1212
final bool? showTimeLeft;
1313
final double? progressBarThumbRadius;
1414
final double? progressBarThumbGlowRadius;
@@ -24,7 +24,7 @@ class Control extends StatefulWidget {
2424

2525
Control({
2626
required this.child,
27-
required this.playerId,
27+
required this.player,
2828
required this.showTimeLeft,
2929
required this.progressBarThumbRadius,
3030
required this.progressBarThumbGlowRadius,
@@ -51,37 +51,37 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
5151
late StreamSubscription<PlaybackState> playPauseStream;
5252
late AnimationController playPauseController;
5353

54+
Player get player => widget.player;
55+
5456
@override
5557
void initState() {
5658
super.initState();
57-
this.playPauseController = new AnimationController(
58-
vsync: this, duration: Duration(milliseconds: 400));
59-
this.playPauseStream = players[widget.playerId]!
60-
.playbackStream
61-
.listen((event) => this.setPlaybackMode(event.isPlaying));
62-
if (players[widget.playerId]!.playback.isPlaying)
63-
this.playPauseController.forward();
59+
playPauseController =
60+
AnimationController(vsync: this, duration: Duration(milliseconds: 400));
61+
playPauseStream = player.playbackStream
62+
.listen((event) => setPlaybackMode(event.isPlaying));
63+
if (player.playback.isPlaying) playPauseController.forward();
6464
}
6565

6666
@override
6767
void dispose() {
68-
this.playPauseStream.cancel();
68+
playPauseStream.cancel();
6969
super.dispose();
7070
}
7171

7272
void setPlaybackMode(bool isPlaying) {
7373
if (isPlaying)
74-
this.playPauseController.forward();
74+
playPauseController.forward();
7575
else
76-
this.playPauseController.reverse();
77-
this.setState(() {});
76+
playPauseController.reverse();
77+
setState(() {});
7878
}
7979

8080
@override
8181
Widget build(BuildContext context) {
8282
return GestureDetector(
8383
onTap: () {
84-
if (players[widget.playerId]!.playback.isPlaying) {
84+
if (player.playback.isPlaying) {
8585
if (_displayTapped) {
8686
setState(() {
8787
_hideControls = true;
@@ -132,7 +132,7 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
132132
padding:
133133
EdgeInsets.only(bottom: 60, right: 20, left: 20),
134134
child: StreamBuilder<PositionState>(
135-
stream: players[widget.playerId]?.positionStream,
135+
stream: player.positionStream,
136136
builder: (BuildContext context,
137137
AsyncSnapshot<PositionState> snapshot) {
138138
final durationState = snapshot.data;
@@ -161,7 +161,7 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
161161
: TimeLabelType.totalTime,
162162
timeLabelTextStyle: widget.progressBarTextStyle,
163163
onSeek: (duration) {
164-
players[widget.playerId]!.seek(duration);
164+
player.seek(duration);
165165
},
166166
),
167167
);
@@ -181,7 +181,7 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
181181
color: Colors.white,
182182
iconSize: 30,
183183
icon: Icon(Icons.skip_previous),
184-
onPressed: () => players[widget.playerId]!.back(),
184+
onPressed: () => player.back(),
185185
),
186186
SizedBox(width: 50),
187187
IconButton(
@@ -190,15 +190,12 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
190190
icon: Icon(Icons.replay_10),
191191
onPressed: () {
192192
int positionInMilliseconds =
193-
players[widget.playerId]!
194-
.position
195-
.position
196-
?.inMilliseconds ??
193+
player.position.position?.inMilliseconds ??
197194
0;
198195
if (!(positionInMilliseconds - 10000)
199196
.isNegative)
200197
positionInMilliseconds -= 10000;
201-
players[widget.playerId]!.seek(Duration(
198+
player.seek(Duration(
202199
milliseconds: positionInMilliseconds));
203200
setState(() {});
204201
}),
@@ -208,16 +205,14 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
208205
iconSize: 30,
209206
icon: AnimatedIcon(
210207
icon: AnimatedIcons.play_pause,
211-
progress: this.playPauseController),
208+
progress: playPauseController),
212209
onPressed: () {
213-
if (players[widget.playerId]!
214-
.playback
215-
.isPlaying) {
216-
players[widget.playerId]!.pause();
217-
this.playPauseController.reverse();
210+
if (player.playback.isPlaying) {
211+
player.pause();
212+
playPauseController.reverse();
218213
} else {
219-
players[widget.playerId]!.play();
220-
this.playPauseController.forward();
214+
player.play();
215+
playPauseController.forward();
221216
}
222217
},
223218
),
@@ -228,21 +223,15 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
228223
icon: Icon(Icons.forward_10),
229224
onPressed: () {
230225
int durationInMilliseconds =
231-
players[widget.playerId]!
232-
.position
233-
.duration
234-
?.inMilliseconds ??
226+
player.position.duration?.inMilliseconds ??
235227
0;
236228
int positionInMilliseconds =
237-
players[widget.playerId]!
238-
.position
239-
.position
240-
?.inMilliseconds ??
229+
player.position.position?.inMilliseconds ??
241230
1;
242231
if ((positionInMilliseconds + 10000) <=
243232
durationInMilliseconds) {
244233
positionInMilliseconds += 10000;
245-
players[widget.playerId]!.seek(Duration(
234+
player.seek(Duration(
246235
milliseconds: positionInMilliseconds));
247236
setState(() {});
248237
}
@@ -252,7 +241,7 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
252241
color: Colors.white,
253242
iconSize: 30,
254243
icon: Icon(Icons.skip_next),
255-
onPressed: () => players[widget.playerId]!.next(),
244+
onPressed: () => player.next(),
256245
),
257246
],
258247
),
@@ -264,7 +253,7 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
264253
crossAxisAlignment: CrossAxisAlignment.end,
265254
children: [
266255
VolumeControl(
267-
playerId: widget.playerId,
256+
player: player,
268257
thumbColor: widget.volumeThumbColor,
269258
inactiveColor: widget.volumeInactiveColor,
270259
activeColor: widget.volumeActiveColor,
@@ -274,7 +263,7 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
274263
iconSize: 24,
275264
icon: Icon(Icons.speaker, color: Colors.white),
276265
onSelected: (Device device) {
277-
players[widget.playerId]!.setDevice(device);
266+
player.setDevice(device);
278267
setState(() {});
279268
},
280269
itemBuilder: (context) {
@@ -305,22 +294,22 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
305294
}
306295

307296
void _cancelAndRestartTimer() {
308-
this._hideTimer?.cancel();
297+
_hideTimer?.cancel();
309298

310-
if (this.mounted) {
311-
this._startHideTimer();
299+
if (mounted) {
300+
_startHideTimer();
312301

313-
this.setState(() {
302+
setState(() {
314303
_hideControls = false;
315304
_displayTapped = true;
316305
});
317306
}
318307
}
319308

320309
void _startHideTimer() {
321-
this._hideTimer = Timer(const Duration(seconds: 3), () {
322-
if (this.mounted) {
323-
this.setState(() {
310+
_hideTimer = Timer(const Duration(seconds: 3), () {
311+
if (mounted) {
312+
setState(() {
324313
_hideControls = true;
325314
});
326315
}
@@ -329,14 +318,14 @@ class ControlState extends State<Control> with SingleTickerProviderStateMixin {
329318
}
330319

331320
class VolumeControl extends StatefulWidget {
332-
final int playerId;
321+
final Player player;
333322
final Color? activeColor;
334323
final Color? inactiveColor;
335324
final Color? backgroundColor;
336325
final Color? thumbColor;
337326

338327
const VolumeControl({
339-
required this.playerId,
328+
required this.player,
340329
required this.activeColor,
341330
required this.inactiveColor,
342331
required this.backgroundColor,
@@ -353,6 +342,8 @@ class _VolumeControlState extends State<VolumeControl> {
353342
bool _showVolume = false;
354343
double unmutedVolume = 0.5;
355344

345+
Player get player => widget.player;
346+
356347
@override
357348
Widget build(BuildContext context) {
358349
return Column(
@@ -387,9 +378,9 @@ class _VolumeControlState extends State<VolumeControl> {
387378
child: Slider(
388379
min: 0.0,
389380
max: 1.0,
390-
value: players[widget.playerId]!.general.volume,
381+
value: player.general.volume,
391382
onChanged: (volume) {
392-
players[widget.playerId]!.setVolume(volume);
383+
player.setVolume(volume);
393384
setState(() {});
394385
},
395386
),
@@ -418,20 +409,20 @@ class _VolumeControlState extends State<VolumeControl> {
418409
}
419410

420411
IconData getIcon() {
421-
if (players[widget.playerId]!.general.volume > .5)
412+
if (player.general.volume > .5)
422413
return Icons.volume_up_sharp;
423-
else if (players[widget.playerId]!.general.volume > 0)
414+
else if (player.general.volume > 0)
424415
return Icons.volume_down_sharp;
425416
else
426417
return Icons.volume_off_sharp;
427418
}
428419

429420
void muteUnmute() {
430-
if (players[widget.playerId]!.general.volume > 0) {
431-
unmutedVolume = players[widget.playerId]!.general.volume;
432-
players[widget.playerId]!.setVolume(0);
421+
if (player.general.volume > 0) {
422+
unmutedVolume = player.general.volume;
423+
player.setVolume(0);
433424
} else {
434-
players[widget.playerId]!.setVolume(unmutedVolume);
425+
player.setVolume(unmutedVolume);
435426
}
436427
setState(() {});
437428
}

lib/src/widgets/video.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ abstract class _VideoStateBase extends State<Video> {
174174
child: widget.showControls
175175
? Control(
176176
key: controlKey,
177-
playerId: playerId,
177+
player: widget.player,
178178
progressBarThumbRadius: widget.progressBarThumbRadius,
179179
progressBarThumbGlowRadius: widget.progressBarThumbGlowRadius,
180180
progressBarActiveColor: widget.progressBarActiveColor,

0 commit comments

Comments
 (0)