I use tasker to emulate a feature that Apple devices have: separate volume levels for speaker and headphones (not sure why Android doesn't already have that).
Set a task that runs when headphones are plugged to set the media volume to x, and an exit task to set it back to y when they're unplugged. Simple.
Set a task that runs when headphones are plugged to set the media volume to x, and an exit task to set it back to y when they're unplugged. Simple.