Warm tip: This article is reproduced from stackoverflow.com, please click
javascript audio progressive-web-apps

PWA javascript event on aux cable removed

发布于 2020-03-27 10:26:30

I'm currently working on a progressive web app to play music (like Spotify or Google Play Music). I came upon a few things I'm not quite sure how to accomplish.

One of them is the feature that the music stopps playing as soon as the aux cable is removed from the phone. Is there a Javascript event or something similar I can attach an event handler to to dispatch the 'pause' action once the aux jack is removed?

Questioner
Tobias Würth
Viewed
33
Anand 2018-04-02 23:45

You will not have such hardware event listener in PWA just with JS.

If you have the option to ask the user to install an service(like some app asks to install barcode scanner service to scan barcode in their app), you can create a helper class like in this solution, which will have an indent filter for aux like in this solution, when detected aux remove event, calls your PWA indent code, which pauses the music.