降低进度条与播放时间的更新频率

新建positionSeconds、sliderPositionSeconds、durationSeconds、bufferedSeconds变量,仅在秒数发生变化时再更新,避免每帧都在重绘控件
This commit is contained in:
orz12
2023-12-19 19:18:06 +08:00
parent 6dd1360a76
commit cd8078a8fa
5 changed files with 91 additions and 53 deletions

BIN
assets/images/live.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 B

View File

@ -236,7 +236,7 @@ class _BangumiPanelState extends State<BangumiPanel> {
children: [ children: [
if (i == currentIndex) ...[ if (i == currentIndex) ...[
Image.asset( Image.asset(
'assets/images/live.gif', 'assets/images/live.png',
color: color:
Theme.of(context).colorScheme.primary, Theme.of(context).colorScheme.primary,
height: 12, height: 12,

View File

@ -46,13 +46,18 @@ class PlPlayerController {
// bool controlsEnabled = false; // bool controlsEnabled = false;
/// 响应数据 /// 响应数据
/// 带有Seconds的变量只在秒数更新时更新以避免频繁触发重绘
// 播放位置 // 播放位置
final Rx<Duration> _position = Rx(Duration.zero); final Rx<Duration> _position = Rx(Duration.zero);
final RxInt positionSeconds = 0.obs;
final Rx<Duration> _sliderPosition = Rx(Duration.zero); final Rx<Duration> _sliderPosition = Rx(Duration.zero);
final RxInt sliderPositionSeconds = 0.obs;
// 展示使用 // 展示使用
final Rx<Duration> _sliderTempPosition = Rx(Duration.zero); final Rx<Duration> _sliderTempPosition = Rx(Duration.zero);
final Rx<Duration> _duration = Rx(Duration.zero); final Rx<Duration> _duration = Rx(Duration.zero);
final RxInt durationSeconds = 0.obs;
final Rx<Duration> _buffered = Rx(Duration.zero); final Rx<Duration> _buffered = Rx(Duration.zero);
final RxInt bufferedSeconds = 0.obs;
final Rx<int> _playerCount = Rx(0); final Rx<int> _playerCount = Rx(0);
@ -225,6 +230,30 @@ class PlPlayerController {
// 播放顺序相关 // 播放顺序相关
PlayRepeat playRepeat = PlayRepeat.pause; PlayRepeat playRepeat = PlayRepeat.pause;
void updateSliderPositionSecond() {
int newSecond = _sliderPosition.value.inSeconds;
if (sliderPositionSeconds.value != newSecond) {
sliderPositionSeconds.value = newSecond;
}
}
void updatePositionSecond() {
int newSecond = _position.value.inSeconds;
if (positionSeconds.value != newSecond) {
positionSeconds.value = newSecond;
}
}
void updateDurationSecond() {
int newSecond = _duration.value.inSeconds;
if (durationSeconds.value != newSecond) {
durationSeconds.value = newSecond;
}
}
void updateBufferedSecond() {
int newSecond = _buffered.value.inSeconds;
if (bufferedSeconds.value != newSecond) {
bufferedSeconds.value = newSecond;
}
}
// 添加一个私有构造函数 // 添加一个私有构造函数
PlPlayerController._() { PlPlayerController._() {
_videoType = videoType; _videoType = videoType;
@ -335,6 +364,7 @@ class PlPlayerController {
dataSource, _looping, enableHA, width, height); dataSource, _looping, enableHA, width, height);
// 获取视频时长 00:00 // 获取视频时长 00:00
_duration.value = duration ?? _videoPlayerController!.state.duration; _duration.value = duration ?? _videoPlayerController!.state.duration;
updateDurationSecond();
// 数据加载完成 // 数据加载完成
dataStatus.status.value = DataStatus.loaded; dataStatus.status.value = DataStatus.loaded;
@ -506,7 +536,7 @@ class PlPlayerController {
element(event ? PlayerStatus.playing : PlayerStatus.paused); element(event ? PlayerStatus.playing : PlayerStatus.paused);
} }
if (videoPlayerController!.state.position.inSeconds != 0) { if (videoPlayerController!.state.position.inSeconds != 0) {
makeHeartBeat(_position.value.inSeconds, type: 'status'); makeHeartBeat(positionSeconds.value, type: 'status');
} }
}), }),
videoPlayerController!.stream.completed.listen((event) { videoPlayerController!.stream.completed.listen((event) {
@ -520,12 +550,14 @@ class PlPlayerController {
} else { } else {
// playerStatus.status.value = PlayerStatus.playing; // playerStatus.status.value = PlayerStatus.playing;
} }
makeHeartBeat(_position.value.inSeconds, type: 'status'); makeHeartBeat(positionSeconds.value, type: 'status');
}), }),
videoPlayerController!.stream.position.listen((event) { videoPlayerController!.stream.position.listen((event) {
_position.value = event; _position.value = event;
updatePositionSecond();
if (!isSliderMoving.value) { if (!isSliderMoving.value) {
_sliderPosition.value = event; _sliderPosition.value = event;
updateSliderPositionSecond();
} }
/// 触发回调事件 /// 触发回调事件
@ -539,6 +571,7 @@ class PlPlayerController {
}), }),
videoPlayerController!.stream.buffer.listen((event) { videoPlayerController!.stream.buffer.listen((event) {
_buffered.value = event; _buffered.value = event;
updateBufferedSecond();
}), }),
videoPlayerController!.stream.buffering.listen((event) { videoPlayerController!.stream.buffering.listen((event) {
isBuffering.value = event; isBuffering.value = event;
@ -580,6 +613,7 @@ class PlPlayerController {
position = Duration.zero; position = Duration.zero;
} }
_position.value = position; _position.value = position;
updatePositionSecond();
_heartDuration = position.inSeconds; _heartDuration = position.inSeconds;
if (duration.value.inSeconds != 0) { if (duration.value.inSeconds != 0) {
if (type != 'slider') { if (type != 'slider') {
@ -667,6 +701,7 @@ class PlPlayerController {
/// 临时fix _duration.value丢失 /// 临时fix _duration.value丢失
if (duration != null) { if (duration != null) {
_duration.value = duration; _duration.value = duration;
updateDurationSecond();
} }
audioSessionHandler.setActive(true); audioSessionHandler.setActive(true);
} }
@ -705,15 +740,17 @@ class PlPlayerController {
/// 调整播放时间 /// 调整播放时间
onChangedSlider(double v) { onChangedSlider(double v) {
_sliderPosition.value = Duration(seconds: v.floor()); _sliderPosition.value = Duration(seconds: v.floor());
updateSliderPositionSecond();
} }
void onChangedSliderStart() { void onChangedSliderStart() {
_isSliderMoving.value = true; _isSliderMoving.value = true;
} }
void onUodatedSliderProgress(Duration value) { void onUpdatedSliderProgress(Duration value) {
_sliderTempPosition.value = value; _sliderTempPosition.value = value;
_sliderPosition.value = value; _sliderPosition.value = value;
updateSliderPositionSecond();
} }
void onChangedSliderEnd() { void onChangedSliderEnd() {

View File

@ -486,7 +486,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
milliseconds: milliseconds:
curSliderPosition + (details.delta.dx * scale).round()); curSliderPosition + (details.delta.dx * scale).round());
Duration result = pos.clamp(Duration.zero, _.duration.value); Duration result = pos.clamp(Duration.zero, _.duration.value);
_.onUodatedSliderProgress(result); _.onUpdatedSliderProgress(result);
_.onChangedSliderStart(); _.onChangedSliderStart();
_initTapPositoin = tapPosition; _initTapPositoin = tapPosition;
}, },
@ -589,9 +589,9 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
/// 进度条 live模式下禁用 /// 进度条 live模式下禁用
Obx( Obx(
() { () {
final int value = _.sliderPosition.value.inSeconds; final int value = _.sliderPositionSeconds.value;
final int max = _.duration.value.inSeconds; final int max = _.durationSeconds.value;
final int buffer = _.buffered.value.inSeconds; final int buffer = _.bufferedSeconds.value;
if (defaultBtmProgressBehavior == if (defaultBtmProgressBehavior ==
BtmProgresBehavior.alwaysHide.code) { BtmProgresBehavior.alwaysHide.code) {
return Container(); return Container();
@ -612,14 +612,6 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
bottom: -3.5, bottom: -3.5,
left: 0, left: 0,
right: 0, right: 0,
child: SlideTransition(
position: Tween<Offset>(
begin: Offset.zero,
end: const Offset(0, -1),
).animate(CurvedAnimation(
parent: animationController,
curve: Curves.easeInOut,
)),
child: ProgressBar( child: ProgressBar(
progress: Duration(seconds: value), progress: Duration(seconds: value),
buffered: Duration(seconds: buffer), buffered: Duration(seconds: buffer),
@ -646,7 +638,16 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
// _.onChangedSlider(duration.inSeconds.toDouble()); // _.onChangedSlider(duration.inSeconds.toDouble());
// _.seekTo(duration); // _.seekTo(duration);
// }, // },
)), ),
// SlideTransition(
// position: Tween<Offset>(
// begin: Offset.zero,
// end: const Offset(0, -1),
// ).animate(CurvedAnimation(
// parent: animationController,
// curve: Curves.easeInOut,
// )),
// child: ),
); );
}, },
), ),

View File

@ -33,9 +33,9 @@ class BottomControl extends StatelessWidget implements PreferredSizeWidget {
children: [ children: [
Obx( Obx(
() { () {
final int value = _.sliderPosition.value.inSeconds; final int value = _.sliderPositionSeconds.value;
final int max = _.duration.value.inSeconds; final int max = _.durationSeconds.value;
final int buffer = _.buffered.value.inSeconds; final int buffer = _.bufferedSeconds.value;
if (value > max || max <= 0) { if (value > max || max <= 0) {
return Container(); return Container();
} }
@ -57,7 +57,7 @@ class BottomControl extends StatelessWidget implements PreferredSizeWidget {
_.onChangedSliderStart(); _.onChangedSliderStart();
}, },
onDragUpdate: (duration) { onDragUpdate: (duration) {
_.onUodatedSliderProgress(duration.timeStamp); _.onUpdatedSliderProgress(duration.timeStamp);
}, },
onSeek: (duration) { onSeek: (duration) {
_.onChangedSliderEnd(); _.onChangedSliderEnd();
@ -78,9 +78,9 @@ class BottomControl extends StatelessWidget implements PreferredSizeWidget {
// 播放时间 // 播放时间
Obx(() { Obx(() {
return Text( return Text(
_.duration.value.inMinutes >= 60 _.durationSeconds.value >= 3600
? printDurationWithHours(_.position.value) ? printDurationWithHours(Duration(seconds: _.positionSeconds.value))
: printDuration(_.position.value), : printDuration(Duration(seconds: _.positionSeconds.value)),
style: textStyle, style: textStyle,
); );
}), }),
@ -89,9 +89,9 @@ class BottomControl extends StatelessWidget implements PreferredSizeWidget {
const SizedBox(width: 2), const SizedBox(width: 2),
Obx( Obx(
() => Text( () => Text(
_.duration.value.inMinutes >= 60 _.durationSeconds.value >= 3600
? printDurationWithHours(_.duration.value) ? printDurationWithHours(Duration(seconds: _.durationSeconds.value))
: printDuration(_.duration.value), : printDuration(Duration(seconds: _.durationSeconds.value)),
style: textStyle, style: textStyle,
), ),
), ),