StreamSubscription<void>? _eventsSubscription;
_eventsSubscription = controller.events.listen((event) {
switch (event) {
casePlaybackStatusChangedEvent():
// Emitted when playback status changes (playing, paused, or stopped)final playbackStatus = controller.playbackStatus;
casePlaybackPositionChangedEvent():
// Emitted when playback position changesfinal position = controller.playbackPosition;
casePlaybackReadyEvent():
// Emitted when video is loaded and ready to playfinal height = controller.videoInfo?.height;
final width = controller.videoInfo?.width;
final duration = controller.videoInfo?.duration;
casePlaybackEndedEvent():
// Emitted when video playback endscasePlaybackSpeedChangedEvent():
// Emitted when playback speed changescaseVolumeChangedEvent():
// Emitted when volume changescasePlaybackErrorEvent():
// Emitted when an error occursprint('Playback error: ${event.errorMessage}');
}
});
// Don't forget to dispose the subscription@overridevoiddispose() {
_eventsSubscription?.cancel();
super.dispose();
}
It's important to properly dispose of both the controller and any event subscriptions when you're done with them to prevent memory leaks. This typically happens in the dispose() method of your widget's State.