From what you said, i strongly belive that this is a hardware fault and the system board needs to be replaced .
This likely to be an issue with the headphone port has got some loose connection.
Please contact your HP technical support to log a warranty repair
Cheers !