DS headphones

170 views
Skip to first unread message

Jason Custer

unread,
Mar 25, 2020, 2:25:20 PM3/25/20
to DroidScript
Hi again!
Got another question for the learned scolars of the group. Yes, I mean you. Is there a way to detect when headphones, bluetooth/wired, are removed? There is an android action I tried to receive, "android.intent.action.HEADSET_PLUG", but I don't receive any intents that aren't specifficly for my app. I also tried using plain javascript, but the changed event doesn't fire. Code is below. I just want to pause audio when headphones are removed. I'm using CreateMediaPlayer.

While we're on the subject, is there a way to catch media buttons when the DS app is not on the screen? IE, checking email, want to pause the music. I'm aware of SetOnKey, but don't think it works when the app doesn't have focus.

Failed JS code.

//this never fires.
navigator.mediaDevices.addEventListener('devicechange', () => {
alert("Device changed.");
navigator.mediaDevices.enumerateDevices()
.then(devices => {
// Check the connected devices
alert(JSON.stringify(devices));
});
});

OnStart = function() {
//this works, sortof, output below.
navigator.mediaDevices.enumerateDevices()
.then(devices => {
// Check the connected devices
alert(JSON.stringify(devices));
});
} //OnStart
//the audioout kind of devices never change, labels are generic, "default".
//output:
[{"deviceId":"default","kind":"audioinput","label":"","groupId":"058f6023048c457fb3c2aeeda368e60baed15063b65054bc3353feee02842c53"},{"deviceId":"4b76aae98398e93c8b633a2f2d60a3d5b2c9ca883e9a19180b48d51c7a74812e","kind":"audioinput","label":"","groupId":"21acdfb4d858ed548d5437cd8b0c9c9f3ce3caae4bb1729e9738f7c0380ae9c7"},{"deviceId":"dfe27e477cb5b2817fb86986d8c92c9b8ed7c629dede9ead4e761d459895a5e6","kind":"audioinput","label":"","groupId":"cf69cb8333f0175dddfc0079ed59af4e54c8d949001e02d2462f3d4a8b70b5ed"},{"deviceId":"c38a32aeb7deae0b55f412ae42682012ba47330c8f9280e3986e90141e6de1ad","kind":"audioinput","label":"","groupId":"ed2e70019013a3760327e55aa3160651ad66fe9ce6cd503756edeadb2f33675e"},{"deviceId":"dfe27e477cb5b2817fb86986d8c92c9b8ed7c629dede9ead4e761d459895a5e6","kind":"videoinput","label":"","groupId":"4901bb3e1486b84be3729fc197ba169b2029d6bfefd4a9f60ce0553d821ba72a"},{"deviceId":"c38a32aeb7deae0b55f412ae42682012ba47330c8f9280e3986e90141e6de1ad","kind":"videoinput","label":"","groupId":"e4bc7b801e55924faf2434ebedf09b62a867b4fb81d1c3cc6cae2ec42f00e597"},{"deviceId":"e5a2c3b65adaec5dd3b1e5e42a2d826136d07fa7381b6f9c927e4e7a918aac1a","kind":"videoinput","label":"","groupId":"dd33f0a32941d57657cd793b489fd6435e794968c35f04ffd054cc51aa12606d"},{"deviceId":"4b76aae98398e93c8b633a2f2d60a3d5b2c9ca883e9a19180b48d51c7a74812e","kind":"videoinput","label":"","groupId":"2c35f647e4e4effc1d9242daf085b8658b809f04e39495b0f0e0482b078a9db1"},{"deviceId":"default","kind":"audiooutput","label":"","groupId":"default"}]

Please forgive the formatting. Extra credit for a working example.
I'd like to do something like this.
onDeviceChange(didRemove) { //onDeviceChange not a real event, yet.
alert("you changed your audio output device. " + didRemove ? "removed" : "added");
}

Dave

unread,
Mar 26, 2020, 9:55:03 PM3/26/20
to DroidScript
In the next release you can do this:-



var callback = function( data ){ alert( JSON.stringify(data)) }
app
.SetOnBroadcast( callback, "android.intent.action.HEADSET_PLUG" )





Message has been deleted
Message has been deleted

Jason Custer

unread,
Mar 27, 2020, 2:55:22 PM3/27/20
to DroidScript
Ok, this sounds very cool, looking forward to experementing with it!

In the meantime, I stepped off the ledge. I wrote my first DS plugin, with AIDE and Java and everything! "I'm a big kid now!"

The plugin, HeadsetDetector, answers my headset needs. It can be found on the spk store, [https://ds.justplayer.de] (https://ds.justplayer.de)

Feedback is appreciated. The plugin is very easy to use, only two functions to worry about, GetStatus, and SetOnChange. Yes, they do what you think.

Thanks for reading!

@Dave, keep up the gooooooood work!!! You are the willy wanka of android. Thanks to our question answerers too!

Dave

unread,
Mar 27, 2020, 9:58:43 PM3/27/20
to androi...@googlegroups.com
Well done for creating your first plugin Jason... you are definitely a big boy now :)

Unfortunately you are not allowed to post binaries to the the SPKstore (for security reasons), so I suggest you delete the apk and the bin and gen folders from the zip and re-upload it.  People can build it them selves using AIDE if they need to use plugin.  Of course, you can post APK's to Google Play if you think that the world needs your plugin, so maybe when you have added a few more utility functions to it you could release it there ;)

Jason Custer

unread,
Jun 7, 2020, 4:00:28 PM6/7/20
to DroidScript
Hi all. Just thought I'd let the group know that there is an update to the plugin I wrote about at the end of march. Originally, the plugin could identify weather a wired audio device was connected/disconnected.

The plugin now offers support for media buttons, IE the bluetooth buttons on headphones, and handles audio focus requests, both from your app, and from other apps.

I had a lot of help, please look at the docs for credits.

Special thanks to Laurent Prott for enabling full bluetooth support and fixing a related bug.

Don't worry, I won't send an email every time something is updated, but in the first email I promiced to update the group when cirtain things had been completed. They've been completed. 😎

Thank you.

Dave

unread,
Jun 7, 2020, 7:25:10 PM6/7/20
to DroidScript
Maybe time to release it on Google Play :)
Reply all
Reply to author
Forward
0 new messages