|
| 1 | +--- |
| 2 | +title: DataCue |
| 3 | +slug: Web/API/DataCue |
| 4 | +page-type: web-api-interface |
| 5 | +status: |
| 6 | + - experimental |
| 7 | +browser-compat: api.DataCue |
| 8 | +--- |
| 9 | + |
| 10 | +{{APIRef("WebVTT")}}{{SeeCompatTable}} |
| 11 | + |
| 12 | +The **`DataCue`** interface represents a cue that associates arbitrary timed data with an audio or video media resource, or exposes timed data from a media resource to web pages. It extends the {{domxref("TextTrackCue")}} interface with a {{domxref("DataCue.value", "value")}} property that can hold any data type, and a {{domxref("DataCue.type", "type")}} property that identifies the kind of data. |
| 13 | + |
| 14 | +Unlike {{domxref("VTTCue")}}, which is designed for displaying subtitle and caption text, `DataCue` is intended for non-rendered timed metadata. Use cases include dynamic content replacement, ad insertion, presentation of supplemental content alongside audio or video, or more generally, triggering application logic at specific points on the media timeline. |
| 15 | + |
| 16 | +Some user agents may also automatically generate `DataCue` objects for in-band timed metadata carried within media streams, such as ID3 tags in [HTTP Live Streaming (HLS)](/en-US/docs/Web/Media/Guides/Audio_and_video_delivery/Setting_up_adaptive_streaming_media_sources#hls_encoding). |
| 17 | + |
| 18 | +{{InheritanceDiagram}} |
| 19 | + |
| 20 | +## Constructor |
| 21 | + |
| 22 | +- {{domxref("DataCue.DataCue", "DataCue()")}} {{experimental_inline}} |
| 23 | + - : Creates a new `DataCue` object. |
| 24 | + |
| 25 | +## Instance properties |
| 26 | + |
| 27 | +_This interface also inherits properties from {{domxref("TextTrackCue")}}._ |
| 28 | + |
| 29 | +- {{domxref("DataCue.type")}} {{ReadOnlyInline}} {{experimental_inline}} |
| 30 | + - : A string identifying the type of the cue's {{domxref("DataCue.value", "value")}}, typically using reverse-domain notation (e.g., `"org.mp4ra"`, `"org.id3"`). |
| 31 | +- {{domxref("DataCue.value")}} {{experimental_inline}} |
| 32 | + - : The data payload associated with the cue. Can be any type. |
| 33 | + |
| 34 | +## Instance methods |
| 35 | + |
| 36 | +_This interface has no methods of its own but inherits methods from {{domxref("TextTrackCue")}}._ |
| 37 | + |
| 38 | +## Examples |
| 39 | + |
| 40 | +### Associating timed metadata with a video |
| 41 | + |
| 42 | +The following example creates a metadata {{domxref("TextTrack")}} on a video element and adds `DataCue` objects containing geolocation coordinates. When each cue becomes active during playback, its {{domxref("TextTrackCue/enter_event", "enter")}} event fires, allowing the page to react — for example, by updating a map view. |
| 43 | + |
| 44 | +```html |
| 45 | +<video controls src="video.mp4"></video> |
| 46 | +``` |
| 47 | + |
| 48 | +```js |
| 49 | +const video = document.querySelector("video"); |
| 50 | +const track = video.addTextTrack("metadata", "Geo Track"); |
| 51 | +track.mode = "hidden"; |
| 52 | + |
| 53 | +const points = [ |
| 54 | + { start: 0, end: 10, data: { latitude: 51.5043, longitude: -0.0762 } }, |
| 55 | + { start: 10, end: 20, data: { latitude: 48.8566, longitude: 2.3522 } }, |
| 56 | + { start: 20, end: 30, data: { latitude: 40.4168, longitude: -3.7038 } }, |
| 57 | +]; |
| 58 | + |
| 59 | +for (const point of points) { |
| 60 | + const cue = new DataCue( |
| 61 | + point.start, |
| 62 | + point.end, |
| 63 | + point.data, |
| 64 | + "org.example.geo", |
| 65 | + ); |
| 66 | + cue.addEventListener("enter", (e) => { |
| 67 | + const { latitude, longitude } = e.target.value; |
| 68 | + console.log(`Map pan to: ${latitude}, ${longitude}`); |
| 69 | + }); |
| 70 | + track.addCue(cue); |
| 71 | +} |
| 72 | + |
| 73 | +// At 0s: "Map pan to: 51.5043, -0.0762" |
| 74 | +// At 10s: "Map pan to: 48.8566, 2.3522" |
| 75 | +// At 20s: "Map pan to: 40.4168, -3.7038" |
| 76 | +``` |
| 77 | + |
| 78 | +## Specifications |
| 79 | + |
| 80 | +{{Specifications}} |
| 81 | + |
| 82 | +## Browser compatibility |
| 83 | + |
| 84 | +{{Compat}} |
| 85 | + |
| 86 | +## See also |
| 87 | + |
| 88 | +- {{domxref("TextTrackCue")}} |
| 89 | +- {{domxref("VTTCue")}} |
| 90 | +- {{domxref("TextTrack")}} |
| 91 | +- {{domxref("TextTrackCue/enter_event", "enter")}} event |
| 92 | +- {{domxref("TextTrackCue/exit_event", "exit")}} event |
0 commit comments