Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,769,820 Members 48,250 Now Online
XDA Developers Android and Mobile Development Forum

How to make headset button control music on the stock ROM

Tip us?
 
interfect
Old
#1  
Junior Member - OP
Thanks Meter 4
Posts: 6
Join Date: Sep 2012
Audio How to make headset button control music on the stock ROM

I'm not sure this is the right subforum for this, but I was coming here to ask how to do this before I figured it out for myself. So I figured I should post about it in case I forget what I did.

I just switched to a shiny new G2, and I'm using the one-button earphones that came with my Nexus S 4G. This is actually the third set of such headphones (I keep breaking and replacing them), so I refuse to have them not work on my rooted but otherwise stock G2.

When I first plugged them in to the G2, it was configured so that the button would control the phone function: one press would open my call history, a double press would redial, and a hold would start up voice dialer. On the Nexus S 4G, the buttons controlled my media player, so that's what I want them to do here.

I tried various button remapping tools like the JAYS Headset Control app, but none of them would take precedence over the default LG actions.

Instead, I modified this approach from the Nexus 4, and changed the key code for the button from the one that the voice dialer controls were intercepting (HEADSETHOOK) to something else (MEDIA_NEXT).

What I did:

1. Have root and install Busybox (for vi).

2. Get a root shell on the device.

Code:
$ adb shell
shell@android:/ $ su
root@android:/ #
3. Mount /system as read-write:

Code:
root@android:/ # mount -o rw,remount /system
4. Edit the default Android keyboard layout to swap the HEADSETHOOK and MEDIA_NEXT keys.

Code:
root@android:/ # vi /system/usr/keylayout/Generic.kl
Press "i" to enter insert mode. Move around with the arrow keys.

Change:

Code:
key 163   MEDIA_NEXT
to:

Code:
key 163   HEADSETHOOK
And also change:

Code:
key 226   HEADSETHOOK
to:

Code:
key 226   MEDIA_NEXT
Press Escape, and then press Shift + Z twice to save and quit.

5. Reboot your device for the changes to take effect.

6. Now the button on your single-button headset is the equivalent of the "down" or "next" button on a three-button headset. To configure it, install JAYS Headset Control or a similar application. Chose the three-button headset instead of the one-button headset, and configure the third button to do everything you want your button to do.

7. Enjoy your customizable button!
The Following 3 Users Say Thank You to interfect For This Useful Post: [ Click to Expand ]
 
Myzur
Old
#2  
Myzur's Avatar
Member
Thanks Meter 1
Posts: 34
Join Date: Jun 2007
Sweet! Not having my headset button work for audio was my biggest issue with the G2.

I followed the instructions above, but instead of swapping "key 163 MEDIA_NEXT" I swapped "key 164 MEDIA_PLAY_PAUSE". This caused the default behavior to be "Play/Pause".

I tried using JAYS Headset Control and it works great. Thanks for the find!
 
copitlory
Old
#3  
Member
Thanks Meter 26
Posts: 71
Join Date: Jun 2013
Oh my gosh! I've been looking everywhere for this solution. Thank you so much!

My dilemma was that I want to be able to use the headset buttons because I listen to music and switch tracks frequently while walking around the university. Only the Android KitKat ROMs worked for me, but the problem with those ROMs is that GPS has locking problems and/or doesn't work at all.
 
ChoppedLiver
Old
#4  
Member
Thanks Meter 9
Posts: 44
Join Date: Dec 2012
Location: Atlanta, GA
Thanks for this, it was helpful for me also. Like Myzur I've also used MEDIA_PLAY_PAUSE instead of MEDIA_NEXT.

I had a quick go with the "Headset Button ControllerTrial" app for some extended functionality, but it kept switching from Rhapsody to LG's music app. This was before I saw the comment about configuring things as if it were a 3 button headset, so I'll play around with this some more (and will probably also try the JAYS app). But even as-is, just the ability to play/pause is a very nice improvement over stock behavior (honestly, who uses their phone more for making calls than listening to music.. ).
 
pabel101
Old
#5  
Junior Member
Thanks Meter 1
Posts: 15
Join Date: Aug 2012
Location: Bronx
Default doesnt work

For some reason this doesnt work with the LG G2 Sprint version, is there any other solution?
 
ChoppedLiver
Old
#6  
Member
Thanks Meter 9
Posts: 44
Join Date: Dec 2012
Location: Atlanta, GA
Quote:
Originally Posted by pabel101 View Post
For some reason this doesnt work with the LG G2 Sprint version, is there any other solution?
My phone is LG G2 on Sprint (LS980), and it actually does work for me.

Since my above post, I have actually changed the mapping again to MEDIA_NEXT, and I'm using the Headset Button Controller app (https://play.google.com/store/apps/d....headsetbutton). The actions in the controller app are all set under "Right button actions" (so exactly according to step 6 in the OP..).

I think any app that will let you configure things for a 3rd/right button should work. The JAYS one is free, but as I recall it asked for a permission I wasn't comfortable with, and I ended up with Kober's app instead, which I think was $2 and to me well worth it (you can barely get a coffee for that these days..). There is a free trial.

This setup works great for music controls (play/pause, next/previous, volume control), but it doesn't seem call control (answer/hangup, mute etc.) is totally working. For me this is mostly about music controls, so I'm not too worried about the call controls and haven't looked into fixing that so far.

By the way, I found out a modified Generic.kl file is enough to cause updates to fail (for ZVC anyway), so you'll want to keep a copy of the original to restore for cases like that.

If you still can't get it to work, what exactly is happening for you, and what app are you using? Did you map MEDIA_NEXT, or MEDIA_PLAY_PAUSE? The Headset Button Controller app I'm using (free trial also) has a troubleshooting feature where it displays the button presses it sees on screen, whether it takes action on it or not, which could be useful to figure things out.
 
pabel101
Old
#7  
Junior Member
Thanks Meter 1
Posts: 15
Join Date: Aug 2012
Location: Bronx
Default doesnt work

Quote:
Originally Posted by ChoppedLiver View Post
My phone is LG G2 on Sprint (LS980), and it actually does work for me.

Since my above post, I have actually changed the mapping again to MEDIA_NEXT, and I'm using the Headset Button Controller app (https://play.google.com/store/apps/d....headsetbutton). The actions in the controller app are all set under "Right button actions" (so exactly according to step 6 in the OP..).

I think any app that will let you configure things for a 3rd/right button should work. The JAYS one is free, but as I recall it asked for a permission I wasn't comfortable with, and I ended up with Kober's app instead, which I think was $2 and to me well worth it (you can barely get a coffee for that these days..). There is a free trial.

This setup works great for music controls (play/pause, next/previous, volume control), but it doesn't seem call control (answer/hangup, mute etc.) is totally working. For me this is mostly about music controls, so I'm not too worried about the call controls and haven't looked into fixing that so far.

By the way, I found out a modified Generic.kl file is enough to cause updates to fail (for ZVC anyway), so you'll want to keep a copy of the original to restore for cases like that.

If you still can't get it to work, what exactly is happening for you, and what app are you using? Did you map MEDIA_NEXT, or MEDIA_PLAY_PAUSE? The Headset Button Controller app I'm using (free trial also) has a troubleshooting feature where it displays the button presses it sees on screen, whether it takes action on it or not, which could be useful to figure things out.
Im sooooo confused by this but is it possible for you to screen shot your settings for Generic.kl? im sorry for the late reply
 
ChoppedLiver
Old
#8  
Member
Thanks Meter 9
Posts: 44
Join Date: Dec 2012
Location: Atlanta, GA
Quote:
Originally Posted by pabel101 View Post
Im sooooo confused by this but is it possible for you to screen shot your settings for Generic.kl? im sorry for the late reply
The whole file is over 400 lines long, so a bit too long to fit in a screenshot. There are only 2 lines that you will need to touch however, and they will look like this when you are done with them:

Code:
key 163   HEADSETHOOK

key 226   MEDIA_NEXT
Note that these lines are not next to each other, but they are easy enough to find as they are pretty much ordered by key "number" as you can see.

Sorry if I made things sound confusing.. the steps in the OP are maybe more clear, and exactly what you need to do to get Generic.kl modified the way mine is. Feel free to ask if you need more help/clarification though.
The Following User Says Thank You to ChoppedLiver For This Useful Post: [ Click to Expand ]
 
pabel101
Old
#9  
Junior Member
Thanks Meter 1
Posts: 15
Join Date: Aug 2012
Location: Bronx
Default playpause

Quote:
Originally Posted by ChoppedLiver View Post
The whole file is over 400 lines long, so a bit too long to fit in a screenshot. There are only 2 lines that you will need to touch however, and they will look like this when you are done with them:

Code:
key 163   HEADSETHOOK

key 226   MEDIA_NEXT
Note that these lines are not next to each other, but they are easy enough to find as they are pretty much ordered by key "number" as you can see.

Sorry if I made things sound confusing.. the steps in the OP are maybe more clear, and exactly what you need to do to get Generic.kl modified the way mine is. Feel free to ask if you need more help/clarification though.
ok i did what you said and now my middle button Just skips to next track is that alright?
 
ChoppedLiver
Old
#10  
Member
Thanks Meter 9
Posts: 44
Join Date: Dec 2012
Location: Atlanta, GA
Quote:
Originally Posted by pabel101 View Post
ok i did what you said and now my middle button Just skips to next track is that alright?
Yes, this means the re-mapping of the button worked. Now you can use one of the headset control apps to program the button as you please (remember this button is now acting as the "next" or right button, so program as such).

But, you say "middle button" - do you have a 3 button headset? If yes, I think you could have left the mapping alone, and programmed either of the left/right buttons, or both. Not really sure of the best and most functional approach in this case, I've never had anything other than a single button headset. I would think with 3 buttons, if all 3 are supported, you can have some more flexibility.

If you do have a 3 button headset, but only the middle button is supported by Android at all (apparently YMMV depending on the make/model among other things), then ignore everything I just said, keep the mapping as-is, and just program the one working button as you like - still as if it were the right button.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes