Node.JS
Node.JS

Reputation: 1570

Headphones prev/next button event detection in Javascript

I am wondering if there is an event or straightforward way to capture prev/next button on headphones other than keydown/keyup that has been mentioned in this answer which I find very hacky.

Something like:

document.on('next_track_event', e => {
    // next song was requested!
});

Upvotes: 2

Views: 643

Answers (1)

Karim A. Wazzan
Karim A. Wazzan

Reputation: 219

Actually there is,

navigator.mediaSession.setActionHandler('previoustrack', function() {
 // User hit "Previous Track" key. 
});

But it's experimental, so not sure about support

Here's a link to know more

https://developer.mozilla.org/en-US/docs/Web/API/MediaSession/setActionHandler

Upvotes: 5

Related Questions