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

implement mediaSession controls for better android integration #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ryanramage
Copy link

It would be amazing if I could pause and do next track with my bluetooth controls with listen-to-transformer. I have tested the mediaSession api, and it easily supports this.

https://googlechrome.github.io/samples/media-session/audio.html

This pull request adds mediaSession controls.

One issue is that the mediaSession api only works on audio tags currently. Understanding the following

  • an audio tag is added to the dom that just plays silence. This is ugly, but it works
  • the audio tag is looped when ended so we can keep audio focus
  • The user has to do a gesture before the silence begins. Once the gesture is performed then chrome will show the playing icon in the status bar.
  • The audio silence needs to be paused and stopped and started to get the remote views in sync with the play/pause state (in the notification control area)

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@ryanramage
Copy link
Author

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

Copy link
Contributor

@notwaldorf notwaldorf left a comment

Choose a reason for hiding this comment

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

Cool idea! Do you know what will happen on desktop browsers like chrome on macOS that have navigator.mediaSession? I am not familiar with this spec at all.

script.js Outdated
if (navigator.mediaSession) {

function playAudio() {
var audio = document.createElement('audio');
Copy link
Contributor

Choose a reason for hiding this comment

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

const

Copy link
Author

Choose a reason for hiding this comment

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

oh good catch. I have added to the pr for all the good style catches. incoming shortly

script.js Outdated

function playAudio() {
var audio = document.createElement('audio');
audio.src = './silence.mp3'; // silence must be at least 5 seconds https://freesound.org/people/silentspeaker/sounds/426895/
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand what this is and why it's needed. Do you have a link to a reference or the spec?

Copy link
Author

Choose a reason for hiding this comment

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

https://developers.google.com/web/updates/2017/02/media-session#implementation_notes

see point 5. specifically

As the Web Audio API doesn't request Android Audio Focus for historical reasons, the only way to make it work with the Media Session API is to hook up an element as the input source to the Web Audio API. Hopefully, the proposed Web AudioFocus API will improve the situation in the near future.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, then can you leave that sentence and the link as a comment, please?

script.js Outdated
audio.src = './silence.mp3'; // silence must be at least 5 seconds https://freesound.org/people/silentspeaker/sounds/426895/

// to keep the controls open, must loop the silence
audio.addEventListener('ended', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

() =>

script.js Outdated
audio.play().then(_ => {
let playing = true;
navigator.mediaSession.setActionHandler('play', function() {
audio.play()
Copy link
Contributor

Choose a reason for hiding this comment

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

semicolons everywhere, () =>

@ryanramage
Copy link
Author

ryanramage commented Feb 18, 2020

ok, here is a cleaned up commit. Style issues addressed, and comments added. Let me know if this works. Thanks!

mediaSession is experimental, so really mainly for Android. see https://caniuse.com/#search=mediaSession
and
https://developers.google.com/web/updates/2017/02/media-session

Chrome on macos pretty much does nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants