Repository navigation
feat: add playback control and state to Video and Audio (#27158) (CP: 25.4) - #27609
Conversation
… 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>
|
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
Diagram Bot draws the mechanism this pull request touches; it does not review the change. Verify it against the diff.
|
|



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