Skip to content

feat: add playback control and state to Video and Audio (#27158) (CP: 25.4) - #27609

Merged
Artur- merged 1 commit into
25.4from
cherry-pick-27158-to-25.4-1796160711540
Dec 1, 2026
Merged

Artur- merged 1 commit into
25.4from
cherry-pick-27158-to-25.4-1796160711540

Conversation

@vaadin-bot

Copy link
Copy Markdown
Collaborator

This PR cherry-picks changes from the original PR #27158 to branch 25.4.


Part of #27155

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

An application can now start, pause, seek and reload a Video or Audio from the server, and see whether the player is playing, also when the visitor used the browser's own controls. A play request the browser refuses can be reported to the application.

NativeButton playIntro = new NativeButton("Play intro",
        event -> video.play(error -> showError(error.code())));
Signal.effect(video, () -> {
    if (video.playbackStateSignal().get() == PlaybackState.ENDED) {
        showReplayButton();
    }
});

Risks:

  • ⚠️ Public API: play(), play(onError), pause(), seek(Duration), load(), playbackStateSignal() and nested types on Media, and a new TestBench MediaElement base for VideoElement and AudioElement.
  • ⚠️ Safari: browsers allow play() with sound only after an interaction of the visitor. Tested in Chromium, Firefox and WebKit: Chromium and Firefox accept a play() that arrives after a server round trip from a click, but WebKit only allows it for a few seconds after the click. A slow round trip can therefore make WebKit refuse a play() from a click listener; it is then reported as NOT_ALLOWED. The Javadoc states this.
  • ⏱️ A player sends play, pause, ended, emptied and seeked events to the server, plus one event on each attach that reports its current state, but only after playbackStateSignal() has been called on it. These events are also accepted while a modal dialog makes the UI inert and while the player is disabled, unlike Flow's default for DOM events, because the player keeps playing in both cases.
  • ✅ No breaking or behaviour change for existing calls; no security, memory, threading or migration concerns.

Context. Playback is started with Media.play(), typically from a click listener. It also covers cases without a click: a muted player, moving on to the next item of a playlist, or a player the visitor has already started. Chose a signal over listeners as the design guidelines ask; the time, duration and volume signals from the issue are left for a follow-up. A click-bound play that runs inside the browser's own click handler, which would avoid the WebKit time limit, is not part of this PR. Starting from a position needs no extra method: a seek() followed by play() in the same listener reaches the browser in one response, in order.

  • Added play(), play(SerializableConsumer<PlaybackError>), pause(), seek(Duration) and load() to Media. play(onError) reports NOT_ALLOWED, NOT_SUPPORTED or UNKNOWN; an interrupted play request (AbortError) is not reported. It runs on the player like its other calls, so it waits with them while the player is hidden. While the player is disabled it runs through the page instead, so that a refusal is reported also then; such a call does not wait while the player is hidden, which the Javadoc states. load() makes changed sources take effect.
  • Documented on play() a single play/pause button and a chapter button that calls seek() and then play(), and that a play/pause label treats PlaybackState.UNKNOWN like PAUSED.
  • Added playbackStateSignal(), a read-only Signal<PlaybackState> (UNKNOWN, PAUSED, PLAYING, ENDED). It is UNKNOWN while detached, and the state is read from the browser on attach.
    • A player whose sources all fail to load stays PLAYING, because the browser reports it as not paused. Chrome does not fail the play request either. This is documented.
  • Added play(), pause() and isPlaying() to TestBench VideoElement and AudioElement through a new MediaElement base.
  • Extended the media unit tests and MediaComponentsIT: play from a click listener, pause, seek and reload from the server, a server play refused before any interaction, a player started in the browser, a seek followed by a play that starts at the new position, the order of seek() and play() while the player is shown, hidden or disabled, and a play request interrupted by a pause, which is not reported as an error, all in Chrome. The test view's audio is now three seconds long instead of half a second, so a player can be seen playing and be paused before it ends; the existing duration check computes its expected value from that length.

API Changes

com.vaadin.flow.component.html.Media

// Added
public void play()
public void play(SerializableConsumer<Media.PlaybackError> onError)
public void pause()
public void seek(Duration position)
public void load()
public Signal<Media.PlaybackState> playbackStateSignal()
public enum Media.PlaybackState { UNKNOWN, PAUSED, PLAYING, ENDED }
public enum Media.PlaybackErrorCode { NOT_ALLOWED, NOT_SUPPORTED, UNKNOWN }
public record Media.PlaybackError(Media.PlaybackErrorCode code, String message) implements Serializable
public interface Media.PlaybackJs extends JsInvokers // pause(), seek(double), load(), reportPlaybackState()
public interface Media.StartPlaybackJs extends JsInvokers // PendingJavaScriptResult play(Element media), run through Element.executeJs, or Page.executeJs while the player is disabled
public interface Media.PlaybackStateJs extends JsSnippets // DomEventData<PlaybackState> readPlaybackState()

com.vaadin.flow.component.html.testbench.MediaElement

// Added
public abstract class MediaElement extends TestBenchElement
public void play()
public void pause()
public boolean isPlaying()

com.vaadin.flow.component.html.testbench.VideoElement / AudioElement

// Changed
- public class VideoElement extends TestBenchElement
+ public class VideoElement extends MediaElement
- public class AudioElement extends TestBenchElement
+ public class AudioElement extends MediaElement

Working on behalf of @Artur-

… 25.4)

Part of #27155

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

An application can now start, pause, seek and reload a `Video` or
`Audio` from the server, and see whether the player is playing, also
when the visitor used the browser's own controls. A play request the
browser refuses can be reported to the application.

```java
NativeButton playIntro = new NativeButton("Play intro",
        event -> video.play(error -> showError(error.code())));
Signal.effect(video, () -> {
    if (video.playbackStateSignal().get() == PlaybackState.ENDED) {
        showReplayButton();
    }
});
```

**Risks:**
- ⚠️ Public API: `play()`, `play(onError)`, `pause()`, `seek(Duration)`,
`load()`, `playbackStateSignal()` and nested types on `Media`, and a new
TestBench `MediaElement` base for `VideoElement` and `AudioElement`.
- ⚠️ Safari: browsers allow `play()` with sound only after an
interaction of the visitor. Tested in Chromium, Firefox and WebKit:
Chromium and Firefox accept a `play()` that arrives after a server round
trip from a click, but WebKit only allows it for a few seconds after the
click. A slow round trip can therefore make WebKit refuse a `play()`
from a click listener; it is then reported as `NOT_ALLOWED`. The Javadoc
states this.
- ⏱️ A player sends play, pause, ended, emptied and seeked events to the
server, plus one event on each attach that reports its current state,
but only after `playbackStateSignal()` has been called on it. These
events are also accepted while a modal dialog makes the UI inert and
while the player is disabled, unlike Flow's default for DOM events,
because the player keeps playing in both cases.
- ✅ No breaking or behaviour change for existing calls; no security,
memory, threading or migration concerns.

**Context.** Playback is started with `Media.play()`, typically from a
click listener. It also covers cases without a click: a muted player,
moving on to the next item of a playlist, or a player the visitor has
already started. Chose a signal over listeners as the design guidelines
ask; the time, duration and volume signals from the issue are left for a
follow-up. A click-bound play that runs inside the browser's own click
handler, which would avoid the WebKit time limit, is not part of this
PR. Starting from a position needs no extra method: a `seek()` followed
by `play()` in the same listener reaches the browser in one response, in
order.

- Added `play()`, `play(SerializableConsumer<PlaybackError>)`,
`pause()`, `seek(Duration)` and `load()` to `Media`. `play(onError)`
reports `NOT_ALLOWED`, `NOT_SUPPORTED` or `UNKNOWN`; an interrupted play
request (`AbortError`) is not reported. It runs on the player like its
other calls, so it waits with them while the player is hidden. While the
player is disabled it runs through the page instead, so that a refusal
is reported also then; such a call does not wait while the player is
hidden, which the Javadoc states. `load()` makes changed sources take
effect.
- Documented on `play()` a single play/pause button and a chapter button
that calls `seek()` and then `play()`, and that a play/pause label
treats `PlaybackState.UNKNOWN` like `PAUSED`.
- Added `playbackStateSignal()`, a read-only `Signal<PlaybackState>`
(`UNKNOWN`, `PAUSED`, `PLAYING`, `ENDED`). It is `UNKNOWN` while
detached, and the state is read from the browser on attach.
- A player whose sources all fail to load stays `PLAYING`, because the
browser reports it as not paused. Chrome does not fail the play request
either. This is documented.
- Added `play()`, `pause()` and `isPlaying()` to TestBench
`VideoElement` and `AudioElement` through a new `MediaElement` base.
- Extended the media unit tests and `MediaComponentsIT`: play from a
click listener, pause, seek and reload from the server, a server play
refused before any interaction, a player started in the browser, a seek
followed by a play that starts at the new position, the order of
`seek()` and `play()` while the player is shown, hidden or disabled, and
a play request interrupted by a pause, which is not reported as an
error, all in Chrome. The test view's audio is now three seconds long
instead of half a second, so a player can be seen playing and be paused
before it ends; the existing duration check computes its expected value
from that length.

## API Changes

### com.vaadin.flow.component.html.Media

```java
// Added
public void play()
public void play(SerializableConsumer<Media.PlaybackError> onError)
public void pause()
public void seek(Duration position)
public void load()
public Signal<Media.PlaybackState> playbackStateSignal()
public enum Media.PlaybackState { UNKNOWN, PAUSED, PLAYING, ENDED }
public enum Media.PlaybackErrorCode { NOT_ALLOWED, NOT_SUPPORTED, UNKNOWN }
public record Media.PlaybackError(Media.PlaybackErrorCode code, String message) implements Serializable
public interface Media.PlaybackJs extends JsInvokers // pause(), seek(double), load(), reportPlaybackState()
public interface Media.StartPlaybackJs extends JsInvokers // PendingJavaScriptResult play(Element media), run through Element.executeJs, or Page.executeJs while the player is disabled
public interface Media.PlaybackStateJs extends JsSnippets // DomEventData<PlaybackState> readPlaybackState()
```

### com.vaadin.flow.component.html.testbench.MediaElement

```java
// Added
public abstract class MediaElement extends TestBenchElement
public void play()
public void pause()
public boolean isPlaying()
```

### com.vaadin.flow.component.html.testbench.VideoElement / AudioElement

```java
// Changed
- public class VideoElement extends TestBenchElement
+ public class VideoElement extends MediaElement
- public class AudioElement extends TestBenchElement
+ public class AudioElement extends MediaElement
```

Working on behalf of @Artur-

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Co-authored-by: Artur Signell <artur@vaadin.com>
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Media.play() sends the request through two different channels depending on whether the player is enabled

flowchart LR
    P["Media.startPlayback()"] --> D{"element.isEnabled()"}
    D -->|"enabled"| E["Element.executeJs(StartPlaybackJs).play()"]
    E -->|"queued with element's own calls, waits while hidden"| H["handlePlayResult()"]
    D -->|"disabled"| Q["Node.runWhenAttached + UI.beforeClientResponse"]:::changed
    Q -->|"queued like element calls, but answers while hidden"| G["Page.executeJs(StartPlaybackJs).play()"]:::changed
    G -->|"answer survives a disabled element"| H
    classDef changed stroke:#c9a227,stroke-width:3px
Loading

Media.play()/play(onError) (Media.java) route the browser play() call differently based on Element.isEnabled(). For an enabled player the call runs through Element.executeJs, queued alongside the element's other calls and waiting while the player is hidden. For a disabled player it instead runs through Node.runWhenAttached and UI.beforeClientResponse into Page.executeJs, because Flow drops the response of a disabled element's own executeJs call — this path still answers while the element is disabled, and (per MediaTest) does not wait while the player is hidden. Both paths converge on the same handlePlayResult() that reports a refusal to the onError callback.

Diagram Bot draws the mechanism this pull request touches; it does not review the change. Verify it against the diff.

Generated by Diagram Bot for #27609 · agent · 52.5 AIC · ⊞ 9.2K · ◷

@Artur-
Artur- enabled auto-merge (squash) December 1, 2026 21:38
@github-actions github-actions Bot added the +0.1.0 label Dec 1, 2026
@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Test Results

 1 655 files   - 30   1 746 suites   - 30   1h 48m 32s ⏱️ - 1m 6s
14 074 tests +17  14 002 ✅ +17  72 💤 ±0  0 ❌ ±0 
14 500 runs   - 46  14 428 ✅  - 46  72 💤 ±0  0 ❌ ±0 

Results for commit fa16c0f. ± Comparison against base commit 35bfb4d.

@Artur-
Artur- merged commit 9d0ac01 into 25.4 Dec 1, 2026
58 checks passed
@Artur-
Artur- deleted the cherry-pick-27158-to-25.4-1796160711540 branch December 1, 2026 21:53
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