Skip to content

Commit

Permalink
Adds functionality to flet_video to know what song is playing (#3772)
Browse files Browse the repository at this point in the history
* Flet video typo fix on on_completed handler

* Flet_video added on_track_changed to get track playing

* Changed event from track to track_changed

* Updated Comment name as well on python end
  • Loading branch information
syleishere authored Aug 8, 2024
1 parent 233bca1 commit a30915f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/flet_video/lib/src/video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class _VideoControlState extends State<VideoControl> with FletStoreMixin {
.triggerControlEvent(widget.control.id, "completed", message ?? "");
}

void _onTrackChanged(String? message) {
debugPrint("Video onTrackChanged: $message");
widget.backend
.triggerControlEvent(widget.control.id, "track_changed", message ?? "");
}

@override
Widget build(BuildContext context) {
debugPrint("Video build: ${widget.control.id}");
Expand Down Expand Up @@ -98,7 +104,7 @@ class _VideoControlState extends State<VideoControl> with FletStoreMixin {

bool onError = widget.control.attrBool("onError", false)!;
bool onCompleted = widget.control.attrBool("onCompleted", false)!;

bool onTrackChanged = widget.control.attrBool("onTrackChanged", false)!;

double? volume = widget.control.attrDouble("volume");
double? pitch = widget.control.attrDouble("pitch");
Expand Down Expand Up @@ -272,8 +278,14 @@ class _VideoControlState extends State<VideoControl> with FletStoreMixin {
_onCompleted(event.toString());
}
});


player.stream.playlist.listen((event) {
if (onTrackChanged) {
_onTrackChanged(event.index.toString());
}
});

return constrainedControl(context, video, widget.parent, widget.control);
});
}
}
}
12 changes: 12 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(
on_exit_fullscreen: OptionalEventCallable = None,
on_error: OptionalEventCallable = None,
on_completed: OptionalEventCallable = None,
on_track_changed: OptionalEventCallable = None,

#
# ConstrainedControl
Expand Down Expand Up @@ -175,6 +176,7 @@ def __init__(
self.on_loaded = on_loaded
self.on_error = on_error
self.on_completed = on_completed
self.on_track_changed = on_track_changed


def _get_control_name(self):
Expand Down Expand Up @@ -601,3 +603,13 @@ def on_completed(self) -> OptionalControlEventCallable:
def on_completed(self, handler: OptionalControlEventCallable):
self._set_attr("onCompleted", True if handler is not None else None)
self._add_event_handler("completed", handler)

# on_track_changed
@property
def on_track_changed(self) -> OptionalControlEventCallable:
return self._get_event_handler("track_changed")

@on_track_changed.setter
def on_track_changed(self, handler: OptionalControlEventCallable):
self._set_attr("onTrackChanged", True if handler is not None else None)
self._add_event_handler("track_changed", handler)

0 comments on commit a30915f

Please sign in to comment.