-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[video_player_avfoundation] removes unnecessary duration and size check before sending the initialized event #9534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
42a5ba4
375ec1f
bb89c41
1ad1d2e
63d240b
0cfe3f4
60fc5d2
f3fbff5
bcc2725
733f5ea
c86f933
35c9a08
3b0cd77
4315c4a
47ff5d0
e2ce9fd
6f232f5
11e8815
f88925e
72b7474
2179d4e
5392116
7769699
5457167
b6ad65a
349f248
48ecaec
ddf7236
a74417f
0d58f8c
42cc71f
04d3a83
ebe1856
f3eeb7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
import 'dart:math' as math show max; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
@@ -79,7 +80,7 @@ class VideoPlayerValue { | |
|
||
/// The total duration of the video. | ||
/// | ||
/// The duration is [Duration.zero] if the video hasn't been initialized. | ||
/// The value is only meaningful when [isInitialized] is true. | ||
final Duration duration; | ||
|
||
/// The current playback position. | ||
|
@@ -430,7 +431,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> { | |
_lifeCycleObserver?.initialize(); | ||
_creatingCompleter = Completer<void>(); | ||
|
||
late DataSource dataSourceDescription; | ||
final DataSource dataSourceDescription; | ||
switch (dataSourceType) { | ||
case DataSourceType.asset: | ||
dataSourceDescription = DataSource( | ||
|
@@ -863,42 +864,37 @@ class VideoPlayer extends StatefulWidget { | |
} | ||
|
||
class _VideoPlayerState extends State<VideoPlayer> { | ||
_VideoPlayerState() { | ||
_listener = () { | ||
final int newPlayerId = widget.controller.playerId; | ||
if (newPlayerId != _playerId) { | ||
setState(() { | ||
_playerId = newPlayerId; | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
late VoidCallback _listener; | ||
|
||
late int _playerId; | ||
void _controllerDidUpdateValue() { | ||
final int newPlayerId = widget.controller.playerId; | ||
if (newPlayerId != _playerId) { | ||
setState(() { | ||
_playerId = newPlayerId; | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_playerId = widget.controller.playerId; | ||
// Need to listen for initialization events since the actual widget ID | ||
// becomes available after asynchronous initialization finishes. | ||
widget.controller.addListener(_listener); | ||
widget.controller.addListener(_controllerDidUpdateValue); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(VideoPlayer oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
oldWidget.controller.removeListener(_listener); | ||
oldWidget.controller.removeListener(_controllerDidUpdateValue); | ||
_playerId = widget.controller.playerId; | ||
widget.controller.addListener(_listener); | ||
widget.controller.addListener(_controllerDidUpdateValue); | ||
} | ||
|
||
@override | ||
void deactivate() { | ||
super.deactivate(); | ||
widget.controller.removeListener(_listener); | ||
void dispose() { | ||
widget.controller.removeListener(_controllerDidUpdateValue); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
|
@@ -1089,58 +1085,52 @@ class VideoProgressIndicator extends StatefulWidget { | |
} | ||
|
||
class _VideoProgressIndicatorState extends State<VideoProgressIndicator> { | ||
_VideoProgressIndicatorState() { | ||
listener = () { | ||
if (!mounted) { | ||
return; | ||
} | ||
setState(() {}); | ||
}; | ||
} | ||
|
||
late VoidCallback listener; | ||
|
||
VideoPlayerController get controller => widget.controller; | ||
|
||
VideoProgressColors get colors => widget.colors; | ||
|
||
void _didUpdateControllerValue() { | ||
setState(() { | ||
// The build method reads from controller.value. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
}); | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
controller.addListener(listener); | ||
controller.addListener(_didUpdateControllerValue); | ||
} | ||
|
||
@override | ||
void deactivate() { | ||
controller.removeListener(listener); | ||
super.deactivate(); | ||
void dispose() { | ||
controller.removeListener(_didUpdateControllerValue); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
Widget progressIndicator; | ||
final Widget progressIndicator; | ||
if (controller.value.isInitialized) { | ||
final int duration = controller.value.duration.inMilliseconds; | ||
final int position = controller.value.position.inMilliseconds; | ||
|
||
int maxBuffering = 0; | ||
for (final DurationRange range in controller.value.buffered) { | ||
final int end = range.end.inMilliseconds; | ||
if (end > maxBuffering) { | ||
maxBuffering = end; | ||
} | ||
} | ||
|
||
final double maxBuffering = | ||
duration == 0.0 | ||
? 0.0 | ||
: controller.value.buffered | ||
.map((DurationRange range) => range.end.inMilliseconds) | ||
.fold(0, math.max) / | ||
duration; | ||
progressIndicator = Stack( | ||
fit: StackFit.passthrough, | ||
children: <Widget>[ | ||
LinearProgressIndicator( | ||
value: maxBuffering / duration, | ||
value: maxBuffering, | ||
valueColor: AlwaysStoppedAnimation<Color>(colors.bufferedColor), | ||
backgroundColor: colors.backgroundColor, | ||
), | ||
LinearProgressIndicator( | ||
value: position / duration, | ||
value: duration == 0.0 ? 0.0 : position / duration, | ||
valueColor: AlwaysStoppedAnimation<Color>(colors.playedColor), | ||
backgroundColor: Colors.transparent, | ||
), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
## 2.8.14 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should revert the CHANGELOG and version change in |
||
|
||
* Fixes an issue in the example app that some widgets stop updating after GlobalKey reparenting. | ||
* Updates the `VideoProgressIndicator` widget in the example app to handle zero-duration videos. | ||
|
||
## 2.8.13 | ||
|
||
* Bumps com.android.tools.build:gradle to 8.12.1. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drive-by fix:
deactivate
is called either when the state is going to be reparented or disposed.