diff --git a/src/content/en/updates/2017/02/media-session.md b/src/content/en/updates/2017/02/media-session.md new file mode 100644 index 00000000000..7b753e5f769 --- /dev/null +++ b/src/content/en/updates/2017/02/media-session.md @@ -0,0 +1,455 @@ +project_path: /web/_project.yaml +book_path: /web/updates/_book.yaml +description: Customize web media notifications and respond to media related events with the new Media Session API. + +{# wf_updated_on: 2017-02-06 #} +{# wf_published_on: 2017-02-06 #} +{# wf_tags: news,chrome57,media,notifications,play #} +{# wf_featured_image: /web/updates/images/2017/02/tldr.png #} +{# wf_featured_snippet: Finally! We can customize web media notifications (title, artist, album name, artwork) and respond to media related events such as seeking or track changing with the new Media Session API. #} + +# Customize Media Notifications and Handle Playlists {: .page-title } + +{% include "web/_shared/contributors/beaufortfrancois.html" %} + +With the brand new [Media Session API], you can now **customize media +notifications** by providing metadata for the media your web app is +playing. It also allows you to **handle media related events** such as seeking +or track changing which may come from notifications or media keys. Excited? Try +out the official [Media Session sample]. + +The Media Session API is supported in Chrome 57 (beta in February 2017, stable +in March 2017). + +
+ Media Session TL;DR; +
Photo by Michael Alø-Nielsen / CC BY 2.0
+
+ +## Gimme what I want + +You already know about the Media Session API and are simply coming back to +copy and paste with no shame some boilerplate code? So here it is. + + if ('mediaSession' in navigator) { + + navigator.mediaSession.metadata = new MediaMetadata({ + title: 'Never Gonna Give You Up', + artist: 'Rick Astley', + album: 'Whenever You Need Somebody', + artwork: [ + { src: 'https://dummyimage.com/96x96', sizes: '96x96', type: 'image/png' }, + { src: 'https://dummyimage.com/128x128', sizes: '128x128', type: 'image/png' }, + { src: 'https://dummyimage.com/192x192', sizes: '192x192', type: 'image/png' }, + { src: 'https://dummyimage.com/256x256', sizes: '256x256', type: 'image/png' }, + { src: 'https://dummyimage.com/384x384', sizes: '384x384', type: 'image/png' }, + { src: 'https://dummyimage.com/512x512', sizes: '512x512', type: 'image/png' }, + ] + }); + + navigator.mediaSession.setActionHandler('play', function() {}); + navigator.mediaSession.setActionHandler('pause', function() {}); + navigator.mediaSession.setActionHandler('seekbackward', function() {}); + navigator.mediaSession.setActionHandler('seekforward', function() {}); + navigator.mediaSession.setActionHandler('previoustrack', function() {}); + navigator.mediaSession.setActionHandler('nexttrack', function() {}); + } + +## Get into the code + +### Let's play 🎷 + +Add a simple `