Welcome to Android Central! That usually means there's a problem with the headphone jack. Check to make sure nothing is stuck inside the jack. If it looks clear, and if the problem persists after a reboot or in Safe Mode, then you probably have to get the headphone jack replaced. If the phone is still under warranty, contact the manufacturer.