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
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h1>Listen to Transformer</h1>
so we made this app to make it easier to explore and curate the model’s output.
</p>

<button class="square" id="btnCloseHelp">start listening</button>
<button class="square interact" id="btnCloseHelp">start listening</button>

<p>If you listen to a bunch of samples, you’ll probably discover that most
compositions (like a lot of fully AI-generated music)
Expand Down Expand Up @@ -127,7 +127,7 @@ <h1 class="outside">
<button title="go to the previous song" id="btnPrevious" disabled>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>
</button>
<button title="play/pause" id="btnPlay">
<button title="play/pause" id="btnPlay" class="interact">
<svg class="no" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>
<svg class="yes" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>
</button>
Expand Down
42 changes: 41 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ function changeSong(index, noAutoplay = false) {
} else {
document.getElementById('btnPrevious').removeAttribute('disabled');
}

pausePlayer(true);
const hash = MODEL + '_' + allData[index].fileName;
window.location.hash = hash;
Expand Down Expand Up @@ -480,3 +479,44 @@ function sketch(p) {
p.rect(x, y, w, h);
}
};

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

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?


// 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.

() =>

this.currentTime = 0;
this.play();
}, false);

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, () =>

});
navigator.mediaSession.setActionHandler('pause', function() {
if (playing) {
audio.pause()
window.pausePlayer()
}
else {
audio.play()
window.startPlayer()
}
playing = !playing
});
navigator.mediaSession.setActionHandler('nexttrack', function() {
window.nextSong()
//store.dispatch(next());
});
}).catch(error => { console.log(error); });
}
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('.interact').forEach(b => {
b.addEventListener('click', playAudio)
})
})
}
Binary file added silence.mp3
Binary file not shown.