diff --git a/index.html b/index.html index 6ba93ec..c4508b1 100644 --- a/index.html +++ b/index.html @@ -64,7 +64,7 @@

Listen to Transformer

so we made this app to make it easier to explore and curate the model’s output.

- +

If you listen to a bunch of samples, you’ll probably discover that most compositions (like a lot of fully AI-generated music) @@ -127,7 +127,7 @@

- diff --git a/script.js b/script.js index 80e0368..2043747 100644 --- a/script.js +++ b/script.js @@ -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; @@ -480,3 +479,50 @@ function sketch(p) { p.rect(x, y, w, h); } }; + +if (navigator.mediaSession) { + + function playAudio() { + + // to keep the controls open, must use a audio element + // We choose a file full of silence (at least 5 seconds), and loop it + // see 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." + + const audio = document.createElement('audio') + audio.src = './silence.mp3' // silence must be at least 5 seconds https://freesound.org/people/silentspeaker/sounds/426895/ + + audio.addEventListener('ended', () => { + this.currentTime = 0 + this.play() + }, false) + + audio.play().then(_ => { + let playing = true + navigator.mediaSession.setActionHandler('play', () => audio.play()) + navigator.mediaSession.setActionHandler('pause', () => { + if (playing) { + audio.pause() + window.pausePlayer() + } + else { + audio.play() + window.startPlayer() + } + playing = !playing + }) + navigator.mediaSession.setActionHandler('nexttrack', () => window.nextSong()) + }).catch(error => console.log(error)) + } + document.addEventListener('DOMContentLoaded', function () { + document.querySelectorAll('.interact').forEach(b => { + b.addEventListener('click', playAudio) + }) + }) +} diff --git a/silence.mp3 b/silence.mp3 new file mode 100644 index 0000000..2030a35 Binary files /dev/null and b/silence.mp3 differ