Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MediaMetadata #2410

Merged
merged 4 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
},
],
},
});

// Expose for tinkering in the console
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
"bugs": {
"url": "https://github.com/sampotts/plyr/issues"
},
"browserslist": "> 1%",
"browserslist": [
"cover 100%",
"not dead"
],
Copy link
Owner

Choose a reason for hiding this comment

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

What's the reason behind this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My IDE (webstorm) showed a warning that string is not a valid option for the browserslist.

Copy link
Contributor Author

@Hashen110 Hashen110 Feb 12, 2022

Choose a reason for hiding this comment

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

Before changing the browserslist

Before changing the browserslist

After changing the browserslist

After changing the browserslist

Copy link
Owner

Choose a reason for hiding this comment

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

Sorry, your IDE has a bug I guess. It's definitely valid and is widely used. The CSS property transition and align-items no longer need prefixing so your first screenshot (before the change) is correct.

"scripts": {
"build": "gulp build",
"lint": "eslint src/js && npm run remark && stylelint **/*.scss",
Expand Down
8 changes: 8 additions & 0 deletions src/js/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ const defaults = {
customControls: true,
noCookie: false, // Whether to use an alternative version of YouTube without cookies
},

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

export default defaults;
15 changes: 15 additions & 0 deletions src/js/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,21 @@ 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) {}
Comment on lines +1764 to +1765
Copy link
Owner

@sampotts sampotts Feb 12, 2022

Choose a reason for hiding this comment

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

Usually, I just do this to avoid disabling the linting rule:

catch(e) {
  // Do nothing
}

},
};

export default controls;
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