onStart method
策略启动,订阅感兴趣的状态变化
Called when the strategy is started. Subscribe to notifiers here.
controller The player widget controller providing state access.
Implementation
@override
void onStart(AliPlayerWidgetController controller) {
// 监听播放状态
listen<int>(controller.playStateNotifier, (state) {
final wasPlaying = _isPlaying;
_isPlaying = (state == FlutterAvpdef.started);
if (_isPlaying && !wasPlaying) {
// 进入播放状态,开始计时
_playStartTimestamp = DateTime.now().millisecondsSinceEpoch;
} else if (!_isPlaying && wasPlaying) {
// 离开播放状态,累计有效播放时长
_accumulatePlayDuration();
}
});
// 监听加载状态
listen<bool>(controller.isLoadingStateNotifier, (isLoading) {
if (isLoading && !_isLoading) {
// 进入 loading
_isLoading = true;
_loadingStartTimestamp = DateTime.now().millisecondsSinceEpoch;
// 暂停有效播放计时
if (_isPlaying) {
_accumulatePlayDuration();
}
} else if (!isLoading && _isLoading) {
// 结束 loading
_isLoading = false;
final loadingDuration = Duration(
milliseconds:
DateTime.now().millisecondsSinceEpoch - _loadingStartTimestamp,
);
// 判断是否为有效卡顿
if (_isPlaying && loadingDuration >= stutterThreshold) {
_stutterCount++;
_totalStutterMs += loadingDuration.inMilliseconds;
final info = _buildStutterInfo();
logi('Stutter detected: $info', tag: name);
onStutterDetected?.call(info);
}
// 恢复有效播放计时
if (_isPlaying) {
_playStartTimestamp = DateTime.now().millisecondsSinceEpoch;
}
}
});
}