-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add WIP Media Session sample #468
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Media Session Sample | ||
=== | ||
See https://googlechrome.github.io/samples/media-session/index.html for a live demo. | ||
|
||
Learn more at https://www.chromestatus.com/feature/5639924124483584 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
feature_name: Media Session | ||
chrome_version: 57 | ||
feature_id: 5639924124483584 | ||
check_min_version: true | ||
--- | ||
|
||
<h3>Background</h3> | ||
<p>The Media Session API lets you customize media notifications by providing | ||
metadata information such as the title, artist, album name, and artwork of the | ||
media (audio or video) 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.</p> | ||
|
||
<button id="playButton">Let's play</button> | ||
|
||
{% include output_helper.html %} | ||
|
||
<script> | ||
if (!('mediaSession' in navigator)) { | ||
ChromeSamples.setStatus('Media Session API is not yet available.'); | ||
} | ||
|
||
// This prevents unnecessary errors when Media Session API is not available. | ||
navigator.mediaSession = navigator.mediaSession || {}; | ||
navigator.mediaSession.setActionHandler = navigator.mediaSession.setActionHandler || function() {}; | ||
window.MediaMetadata = window.MediaMetadata || function() {}; | ||
</script> | ||
|
||
{% include js_snippet.html filename='index.js' %} | ||
|
||
<script> | ||
log = ChromeSamples.log; | ||
|
||
document.querySelector('#playButton').addEventListener('click', onPlayButtonClick); | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
let audio = document.createElement('audio'); | ||
|
||
let playlist = getPlaylist(); | ||
let index = 0; | ||
|
||
function onPlayButtonClick(event) { | ||
playAudio(); | ||
} | ||
|
||
function playAudio() { | ||
audio.src = playlist[index].src; | ||
audio.play() | ||
.then(_ => updateMetadata()) | ||
.catch(error => log(error)); | ||
} | ||
|
||
function updateMetadata() { | ||
let track = playlist[index]; | ||
|
||
log('Playing "' + track.title + '" track...'); | ||
navigator.mediaSession.metadata = new MediaMetadata({ | ||
title: track.title, | ||
artist: track.artist, | ||
album: track.album, | ||
artwork: track.artwork | ||
}); | ||
} | ||
|
||
/* Previous Track & Next Track */ | ||
|
||
navigator.mediaSession.setActionHandler('previoustrack', function() { | ||
log('> User clicked "Previous Track" icon.'); | ||
index = (index - 1 + playlist.length) % playlist.length; | ||
playAudio(); | ||
}); | ||
|
||
navigator.mediaSession.setActionHandler('nexttrack', function() { | ||
log('> User clicked "Next Track" icon.'); | ||
index = (index + 1) % playlist.length; | ||
playAudio(); | ||
}); | ||
|
||
/* Seek Backward & Seek Forward */ | ||
|
||
let skipTime= 10; /* Time to skip in seconds */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: space before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for noticing! |
||
|
||
navigator.mediaSession.setActionHandler('seekbackward', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I be able to see the seeking controls? I only get previous/next on Android. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
log('> User clicked "Seek Backward" icon.'); | ||
audio.currentTime = Math.max(audio.currentTime - skipTime, 0); | ||
}); | ||
|
||
navigator.mediaSession.setActionHandler('seekforward', function() { | ||
log('> User clicked "Seek Forward" icon.'); | ||
audio.currentTime = Math.min(audio.currentTime + skipTime, audio.duration); | ||
}); | ||
|
||
/* Utils */ | ||
|
||
function getPlaylist() { | ||
return [{ | ||
src: 'jam.ogg', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not crucial, but it would be really nice to have a different audio file for each track :). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @samdutton That is my main issue there. Instead of having to upload one single audio file, I'd like to use some CC audio files from our server. Do you know if we already have some? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah... I see. I have one short recording I did of me playing guitar: https://simpl.info/audio/audio/audio.mp3. Of course, you'll still need to add a licence. (I probably should add something to simpl.info, even though it's my recording.) I guess I could do four more if need be (if you can face more of my guitar playing :). Alternatively, I just found these Sintel recordings (used by Dale Curtis for his MSE gapless playback article: https://developers.google.com/web/updates/2015/06/Media-Source-Extensions-for-Audio): https://github.com/samdutton/simpl/tree/gh-pages/mse/audio/audio |
||
title: 'Title #1', | ||
artist: 'Artist #1', | ||
album: 'Album #1', | ||
artwork: getArtwork('1') | ||
}, { | ||
src: 'jam.ogg', | ||
title: 'Title #2', | ||
artist: 'Artist #2', | ||
album: 'Album #2', | ||
artwork: getArtwork('2') | ||
}, { | ||
src: 'jam.ogg', | ||
title: 'Title #3', | ||
artist: 'Artist #3', | ||
album: 'Album #3', | ||
artwork: getArtwork('3') | ||
}, { | ||
src: 'jam.ogg', | ||
title: 'Title #4', | ||
artist: 'Artist #4', | ||
album: 'Album #4', | ||
artwork: getArtwork('4') | ||
}, { | ||
src: 'jam.ogg', | ||
title: 'Title #5', | ||
artist: 'Artist #5', | ||
album: 'Album #5', | ||
artwork: getArtwork('5') | ||
}]; | ||
} | ||
|
||
function getArtwork(text) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be inclined just to have these as local images — the Having said that, it's nice to show that you can get a remote image file. Hmm... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah... I know ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return [{ | ||
src: 'https://dummyimage.com/96x96?text='+text, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: spaces around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
sizes: '96x96', | ||
type: 'image/png' | ||
}, { | ||
src: 'https://dummyimage.com/128x128?text='+text, | ||
sizes: '128x128', | ||
type: 'image/png' | ||
}, { | ||
src: 'https://dummyimage.com/192x192?text='+text, | ||
sizes: '192x192', | ||
type: 'image/png' | ||
}, { | ||
src: 'https://dummyimage.com/256x256?text='+text, | ||
sizes: '256x256', | ||
type: 'image/png' | ||
}, { | ||
src: 'https://dummyimage.com/384x384?text='+text, | ||
sizes: '384x384', | ||
type: 'image/png' | ||
}, { | ||
src: 'https://dummyimage.com/512x512?text='+text, | ||
sizes: '512x512', | ||
type: 'image/png' | ||
}]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: could maybe remove
event
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.