Repository navigation
feat: add time, duration, muted and volume signals to Video and Audio - #27579
totally-not-ai[bot] wants to merge 60 commits into
Conversation
Adds play(), pause() and seek() to Media, a play(onError) overload that reports a refusal by the browser, and playbackStateSignal() that follows what the player in the browser does. TestBench VideoElement and AudioElement get play(), pause() and isPlaying() through a common MediaElement base.
Runs the playback IT on a data URL audio so that seeking really works, and documents that a player whose sources all fail stays PLAYING.
Replace the server-side play() with Media.onClick(component).play(media), which calls play() inside the browser's click handler. Safari only allows a player with sound to start for a few seconds after a click, so a play request sent back from a server-side click listener fails when the round trip is slow; the click-bound action does not depend on the round trip. The integration test no longer checks that a play() request sent when the view opens is refused, since there is no server-side play() to send it. Also declare the playback JavaScript as JsInvokers and JsSnippets, which executeJs(Class) and JsSnippets.of(Class) now require.
The click handler captures the DOM node of the player, and a player that is attached again gets a new node, so the handler would start a node that is no longer on the page. Install the handler on each attach of the player and remove it on detach. Also reword the setControls Javadoc, which still suggested hiding the controls for a sound the application starts on its own.
Reinstalling the click handler on each attach of the player built a new action every time, and each one registered another return channel on the clicked component that nothing removed. The binding now keeps one trigger and one action per play(...) call and installs them again, so the action keeps using the channel it already registered. The trigger is created in Media.onClick(...) again, so an onClick(...) without a play(...) is reported like the other click bindings do.
Also always wire the first install through the deferred trigger overload, which waits for the next attach when play(...) is called while the player is being detached.
The browser's ended flag says the position is at the end, not that the playback ran there, so seeking a paused player to the end reports ENDED.
Seek a paused player past the end and wait for ENDED, then move it back and wait for PAUSED, so both transitions come from the seeked event.
The listeners that reinstall the click handler when the player is reattached hold the trigger and, through it, the clicked component, so a player that outlived its play buttons kept every one of them. They are now registered only while the button is attached, and the handler is installed again when the button is. Also check the exact event data the playback state listeners send.
Arm the trigger once with the deferred install, and afterwards install the handler directly or from the player's attach listener, so detaching the button leaves no listener on the player that refers to it. Replace the button-reattach test, which passed without the fix, with one that checks the detached button can be garbage collected and one that checks the reattached button follows the player again.
Arming through the player left a one-shot attach listener on a player that was not attached yet, which kept the button after it was removed. Arm on the clicked component instead and let rewire() install the handler. Cover binding before either component is attached, both attach orders, and reattaching the button while the player is away.
Also drop a reattach check that another test already covers, and describe the clicked component's lifecycle in the Wiring comment.
A player keeps playing while a modal dialog makes the UI inert or while it is disabled, but its playback events were dropped then, so the state signal could stay PLAYING after the recording ended. Accept the events in both cases.
The state read on attach came back as a JavaScript return value, which is dropped for a disabled component, so a disabled player that was already playing stayed UNKNOWN. The browser now fires an event on the player that the state listener receives, also while disabled. A DOM event never reaches a detached component, so the check that a late answer after detach is ignored is gone with the return value. Also reset the player-detached flag when the clicked component is attached again, which could install the click handler twice when both were reattached together.
The public reportPlaybackState() no longer takes an event type, so it can only make the player report its state.
Dragging the browser's timeline fires seeked over and over, and each one was a round trip. Send it once the position settles; the client sends a pending debounced event before any other event, so the states still arrive in order.
The tests no longer need to null out a local variable to let the button be collected.
Also state in the play(onError) Javadoc what is not covered for a disabled player: one disabled only after the call, and a disabled one that is hidden.
currentTimeSignal() reports a Position: Unknown until the browser has reported it, or Known with the position, throttled while playing to a configurable interval (one second by default) and exact on pause, seek, end and reload. durationSignal() reports a Length: Unknown until the browser knows it, Live for a live stream, or Known with the length. mutedSignal() and volumeSignal() follow the player, also when the visitor changes the sound through the browser's controls: the muted and volume properties are synchronized from the browser, so isMuted() and getVolume() return the visitor's choice, a later setMuted() or setVolume() can undo it, and nothing is sent back to the browser to overwrite a newer change. setVolume()/getVolume() set the volume from the server. Like playbackStateSignal(), each signal only listens to the player once it is asked for, reads the player's state when asked for, and follows the player behind a modal dialog and while disabled. The position and length are read again on each attach; muted and volume need not be, as the server applies its values to the new element. seeked is debounced, since dragging the browser's timeline fires it over and over. volumechange is not: a debounced or throttled event loses the property values the client buffers for it. The listener and state-request plumbing is shared by all signals, and the requests of several signals in one round trip are merged. PlaybackStateJs is renamed to PlayerStateJs and PlaybackJs.reportPlaybackState() to reportState(), since they now serve every state signal, not only the playback state.
setVolume needs a double test value. setTimeUpdateInterval is a server-side setting the element does not hold, so the smoke test's check that a setter changes the element does not apply; MediaTest covers it.
Name the repeated emptied event, rename the length signal field so it no longer differs from the DURATION constant only in case, and use Math.clamp for the volume.
That test belongs to the click binding, which this PR does not change; the stricter message check had come in with the signals by mistake. Also drop a comment that restated the NaN check in toDuration, which is now written out instead of hidden in a negated comparison.
|
Fixed the Sonar findings that apply:
The |
A static factory on a component class that returns a binding does not match an API pattern used for components, so the click-bound play is left out. Playback is started with Media.play(), typically from a click listener. The play/pause toggle recipe moves to the play() Javadoc, and the tests of the click binding are removed together with it. The error name mapping they covered is now tested through play(onError).
The play button of the test view now calls play() from the server, so the second half of the autoplay test repeated the main playback test. Also note in the NOT_ALLOWED Javadoc that Safari allows a play from a click listener only for a few seconds after the click.
…o feat/media-time-volume-signals # Conflicts: # flow-html-components/src/main/java/com/vaadin/flow/component/html/Media.java # flow-html-components/src/test/java/com/vaadin/flow/component/html/MediaTest.java # flow-tests/test-default/src/main/java/com/vaadin/flow/test/components/MediaComponentsView.java
A seek() followed by play() in the same listener reaches the browser in one response, so the player starts from the new position without a separate play-from-position method. Document this on play() and cover it with a browser test that checks the position when playback starts.
The browser test cannot tell the order apart, since play() starts asynchronously, so the unit test now checks that a seek() before play() is sent first, also while the player is hidden or disabled. Name the disabled-and-hidden exception in the Javadoc, and wait for the play event rather than the short PLAYING state in the browser test.
…o feat/media-time-volume-signals # Conflicts: # flow-tests/test-default/src/main/java/com/vaadin/flow/test/components/MediaComponentsView.java
…e-signals # Conflicts: # flow-html-components/src/main/java/com/vaadin/flow/component/html/Media.java # flow-html-components/src/test/java/com/vaadin/flow/component/html/MediaTest.java # flow-tests/test-default/src/main/java/com/vaadin/flow/test/components/MediaComponentsView.java # flow-tests/test-default/src/test/java/com/vaadin/flow/test/components/MediaComponentsIT.java
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 1 comment.
| Finding | |
|---|---|
| 📝 | setVolume omits the "setting the same value again does nothing" caveat that setMuted spells out |
| * if the volume is not between <code>0</code> and | ||
| * <code>1</code> | ||
| */ | ||
| public void setVolume(double volume) { |
There was a problem hiding this comment.
📝 setVolume omits the "setting the same value again does nothing" caveat that setMuted spells out
setMuted documents the whole trap: until a sound signal is asked for, the visitor's change never reaches the server, so setting the value the server already holds is a silent no-op, and the server value is re-applied when the browser creates the element anew.
setVolume behaves identically but only says "The visitor can still change it through the browser's controls; volumeSignal() follows that." A caller who sets 0.5, lets the visitor drag the slider to 0.9 and then calls setVolume(0.5) to reset gets nothing, and nothing in this method's Javadoc explains why. getVolume() carries half of the story; the setter should carry the other half, as its sibling does.
Media.java:1090 · documentation
There was a problem hiding this comment.
Added it. The setVolume Javadoc now matches setMuted. It says that a volume the visitor sets through the browser's controls reaches the server only once volumeSignal() or mutedSignal() has been asked for. Until then, getVolume() returns the server's value, and setting that same value again does not undo the visitor's change. It also says that the value is applied again when the browser creates the element anew. No code change.
Like setMuted, a volume the visitor sets through the browser's controls reaches the server only once a sound signal is in use, so setting the value the server already holds does nothing until then.
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
|



Part of #27155
new feature · flow-html-components · apps that use
VideoorAudioFollows #27158, which is now merged.
A
VideoorAudionow reports its position, its length, and whether it is muted and how loud, as signals. They also follow the visitor's own use of the browser's controls, so custom controls such as a time display, a chapter list or a mute button can be built as bindings, without raw DOM listeners.Use case
A course platform shows lectures with its own control bar: elapsed and total time, and a mute toggle that stays right when the visitor mutes through the browser's controls.
Risks:
setVolume/getVolume,setTimeUpdateInterval/getTimeUpdateInterval, the sealedMedia.PositionandMedia.Lengthtypes, and two more event-data methods on the JavaScript definition.PlaybackStateJsandPlaybackJs.reportPlaybackState()from feat: add playback control and state to Video and Audio #27158 are renamed toPlayerStateJsandreportState(), since they now serve every signal; neither has been released.currentTimeSignal()is in use sends its position once per interval (one second by default). OncemutedSignal()orvolumeSignal()is in use, everyvolumechangeis sent. Nothing is sent for a signal that has not been asked for.mutedSignal()orvolumeSignal()is asked for:isMuted()andgetVolume()then return the visitor's choice made through the browser's controls, so a latersetMuted()/setVolume()can undo it.What changed
currentTimeSignal()→Signal<Media.Position>:Unknownuntil the browser reports it, thenKnown(Duration). Updated at most once persetTimeUpdateInterval(...)while playing (throttledtimeupdate), and exactly on pause, seek, end and reload.durationSignal()→Signal<Media.Length>:Unknownuntil the browser knows the length,Livefor a live stream,Known(Duration)otherwise.mutedSignal()andvolumeSignal()followsetMuted/setVolumeand the visitor's changes. Themutedandvolumeproperties are synchronized from the browser rather than set on the server, so nothing is sent back that could overwrite a newer change by the visitor.volumechangeis not debounced or throttled: a delayed event loses the property values the client buffers for it. A value from the browser outside 0–1 is clamped.setVolume(double)/getVolume(), 0 to 1.playbackStateSignal(), each signal listens only once asked for, asks the player for its state when asked for, and keeps following the player behind a modal dialog and while it is disabled. Position and length are read again on each attach. Requests from several signals in one round trip are merged into one, and a request dropped because the component moved to another UI does not block later ones.Test summary
API Changes
com.vaadin.flow.component.html.Media