Skip to content

feat: add time, duration, muted and volume signals to Video and Audio - #27579

Draft
totally-not-ai[bot] wants to merge 60 commits into
mainfrom
feat/media-time-volume-signals
Draft

totally-not-ai[bot] wants to merge 60 commits into
mainfrom
feat/media-time-volume-signals

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Part of #27155

new feature · flow-html-components · apps that use Video or Audio

Follows #27158, which is now merged.

A Video or Audio now 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.

Span elapsed = new Span(lecture.currentTimeSignal().map(position ->
        position instanceof Media.Position.Known known
                ? format(known.position()) : "--:--"));
NativeButton mute = new NativeButton();
Signal.effect(mute, () -> mute.setText(
        lecture.mutedSignal().get() ? "Unmute" : "Mute"));
mute.addClickListener(event -> lecture.setMuted(!lecture.mutedSignal().peek()));

Risks:

  • ⚠️ Public API: four signal accessors, setVolume/getVolume, setTimeUpdateInterval/getTimeUpdateInterval, the sealed Media.Position and Media.Length types, and two more event-data methods on the JavaScript definition. PlaybackStateJs and PlaybackJs.reportPlaybackState() from feat: add playback control and state to Video and Audio #27158 are renamed to PlayerStateJs and reportState(), since they now serve every signal; neither has been released.
  • ⏱️ While playing, a player whose currentTimeSignal() is in use sends its position once per interval (one second by default). Once mutedSignal() or volumeSignal() is in use, every volumechange is sent. Nothing is sent for a signal that has not been asked for.
  • ↔️ Behaviour change, only once mutedSignal() or volumeSignal() is asked for: isMuted() and getVolume() then return the visitor's choice made through the browser's controls, so a later setMuted()/setVolume() can undo it.

What changed

  • currentTimeSignal() → Signal<Media.Position>: Unknown until the browser reports it, then Known(Duration). Updated at most once per setTimeUpdateInterval(...) while playing (throttled timeupdate), and exactly on pause, seek, end and reload.
  • durationSignal() → Signal<Media.Length>: Unknown until the browser knows the length, Live for a live stream, Known(Duration) otherwise.
  • mutedSignal() and volumeSignal() follow setMuted/setVolume and the visitor's changes. The muted and volume properties 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. volumechange is 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.
  • Like 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

  • Unit tests for each signal: listening only once asked for, the events each one follows and their throttle or debounce settings, reset on detach, merged state requests, the visitor's sound changes and the server undoing them, and the value checks.
  • Browser tests in Chrome: the position while playing and at a pause, the length, and muted and volume changed from the server and by the visitor, including the server undoing the visitor's mute.

API Changes

com.vaadin.flow.component.html.Media

// Added
public Signal<Media.Position> currentTimeSignal()
public void setTimeUpdateInterval(Duration interval)
public Duration getTimeUpdateInterval()
public Signal<Media.Length> durationSignal()
public Signal<Boolean> mutedSignal()
public void setVolume(double volume)
public double getVolume()
public Signal<Double> volumeSignal()
public sealed interface Media.Position extends Serializable // record Unknown(), record Known(Duration position)
public sealed interface Media.Length extends Serializable // record Unknown(), record Live(), record Known(Duration duration)

// Changed (both added in #27158, not released)
- public interface Media.PlaybackStateJs extends JsSnippets // readPlaybackState()
+ public interface Media.PlayerStateJs extends JsSnippets // readPlaybackState(), readCurrentTime(), readDuration()
- void PlaybackJs.reportPlaybackState()
+ void PlaybackJs.reportState()

totally-not-ai Bot and others added 30 commits November 22, 2026 16:54
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.
@github-actions github-actions Bot added the +1.0.0 label Dec 1, 2026
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.
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 655 files  ± 0   1 746 suites  ±0   1h 44m 57s ⏱️ - 1m 32s
14 090 tests +16  14 018 ✅ +16  72 💤 ±0  0 ❌ ±0 
14 516 runs  +16  14 444 ✅ +16  72 💤 ±0  0 ❌ ±0 

Results for commit 5fbce6a. ± Comparison against base commit 1e15ac4.

♻️ This comment has been updated with latest results.

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.
@totally-not-ai

totally-not-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the Sonar findings that apply:

  • The repeated "emptied" event name is now a constant.
  • The length signal's field is renamed, so it no longer differs from the DURATION constant only in case.
  • getVolume() uses Math.clamp, and its local no longer shadows a field.
  • The position conversion checks for NaN explicitly instead of through a negated comparison.

The java:S6665 findings point at nested types that carry no @NullMarked. Only the top-level class has it, as on main, so there is nothing to remove there.

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
Base automatically changed from feat/media-playback-control to main December 1, 2026 21:31
…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
@Artur-
Artur- requested review from vaadin-review-bot and removed request for vaadin-review-bot December 2, 2026 05:48

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants