Skip to content

Commit

Permalink
feat: add MediaMetadata (#2410)
Browse files Browse the repository at this point in the history
* change browserslist to cover 100% not dead browsers

* feat: add MediaMetadata

* Revert browserslist change

Co-authored-by: Sam Potts <[email protected]>
  • Loading branch information
Hashen110 and sampotts authored Feb 24, 2022
1 parent 4632614 commit 6bc447b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ Note the single quotes encapsulating the JSON and double quotes on the object ke
| `vimeo` | Object | `{ byline: false, portrait: false, title: false, speed: true, transparent: false }` | See [Vimeo embed options](https://github.com/vimeo/player.js/#embed-options). Some are set automatically based on other config options, namely: `loop`, `autoplay`, `muted`, `gesture`, `playsinline` |
| `youtube` | Object | `{ noCookie: false, rel: 0, showinfo: 0, iv_load_policy: 3, modestbranding: 1 }` | See [YouTube embed options](https://developers.google.com/youtube/player_parameters#Parameters). The only custom option is `noCookie` to use an alternative to YouTube that doesn't use cookies (useful for GDPR, etc). Some are set automatically based on other config options, namely: `autoplay`, `hl`, `controls`, `disablekb`, `playsinline`, `cc_load_policy`, `cc_lang_pref`, `widget_referrer` |
| `previewThumbnails` | Object | `{ enabled: false, src: '' }` | `enabled`: Whether to enable the preview thumbnails (they must be generated by you). `src` must be either a string or an array of strings representing URLs for the VTT files containing the image URL(s). Learn more about [preview thumbnails](#preview-thumbnails) below. |
| `mediaMetadata` | Object | `{ title: '', artist: '', album: '', artwork: [] }` | The [MediaMetadata](https://developer.mozilla.org/en-US/docs/Web/API/MediaMetadata) interface of the Media Session API allows a web page to provide rich media metadata for display in a platform UI. |

1. Vimeo only
2. Autoplay is generally not recommended as it is seen as a negative user experience. It is also disabled in many browsers. Before raising issues, do your homework. More info can be found here:
Expand Down
11 changes: 11 additions & 0 deletions demo/src/js/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ import toggleClass from './toggle-class';
// Prevent Vimeo blocking plyr.io demo site
referrerPolicy: 'no-referrer',
},
mediaMetadata: {
title: 'View From A Blue Moon',
album: 'Sports',
artist: 'Brainfarm',
artwork: [
{
src: 'https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.jpg',
type: 'image/jpeg',
},
],
},
markers: {
enabled: true,
points: [
Expand Down
10 changes: 9 additions & 1 deletion src/js/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,15 @@ const defaults = {
noCookie: false, // Whether to use an alternative version of YouTube without cookies
},

// markers
// Media Metadata
mediaMetadata: {
title: '',
artist: '',
album: '',
artwork: [],
},

// Markers
markers: {
enabled: false,
points: [],
Expand Down
17 changes: 17 additions & 0 deletions src/js/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,23 @@ const controls = {
});
}
},

// Set media metadata
setMediaMetadata() {
try {
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new window.MediaMetadata({
title: this.config.mediaMetadata.title,
artist: this.config.mediaMetadata.artist,
album: this.config.mediaMetadata.album,
artwork: this.config.mediaMetadata.artwork,
});
}
// eslint-disable-next-line no-empty
} catch (e) {}
},

// Add markers
setMarkers() {
if (this.duration > 0 && !this.elements.markers) {
const { points } = this.config.markers;
Expand Down
18 changes: 18 additions & 0 deletions src/js/plyr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ declare namespace Plyr {
* Preview Thumbnails Options.
*/
previewThumbnails?: PreviewThumbnailsOptions;

/**
* Media Metadata Options.
*/
mediaMetadata?: MediaMetadataOptions;
}

interface QualityOptions {
Expand Down Expand Up @@ -576,6 +581,19 @@ declare namespace Plyr {
src?: string | string[];
}

interface MediaMetadataArtwork {
src: string;
sizes?: string;
type: string;
}

interface MediaMetadataOptions {
title?: string;
artist?: string;
album?: string;
artwork?: MediaMetadataArtwork[];
}

export interface Elements {
buttons: {
airplay?: HTMLButtonElement;
Expand Down
5 changes: 5 additions & 0 deletions src/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ const ui = {
if (this.config.duration) {
controls.durationUpdate.call(this);
}

// Media metadata
if (this.config.mediaMetadata) {
controls.setMediaMetadata.call(this);
}
},

// Setup aria attribute for play and iframe title
Expand Down

0 comments on commit 6bc447b

Please sign in to comment.