A lot of users are facing noise problem when headphones are connected There are a lot of solutions given on Ask Ubuntu but none have worked for me.
I didn't have this problem when I used Windows 8.1, but I don't want to go back to Windows.
I have accepted the fact that my laptop's sound system is not shielded properly.
I haven't addressed the root of the problem, but just kind of made it a virtually unnoticable.
So, the hack is quite simple. As the problem persists only when there is no playback, I came up with a hack to always have a playback!
Now, this should be true only when the headphones are connected. And the playback should stop when headphones are removed.
Follow these simple steps:
Step #1: Install mplayer and configure it.
sudo apt install mplayer
nano ~/.mplayer/config
type in that file
lirc=no
save and exit.
Step #2: Download this mp3 file silence-10sec.mp3.
Step #3: Create a folder in home directory and name it .hush
Copy the mp3 file in your .hush
directory
cp ~/Downloads/silence-10sec.mp3 ~/.hush/
In that folder, create a script file and name it silence.sh
Copy paste this code in the script file.
#!/bin/bash
plugged="jack/headphone HEADPHONE plug"
unplugged="jack/headphone HEADPHONE unplug"
acpi_listen | while IFS= read -r event;
do
if [ "$event" == "$plugged" ]
then
mplayer -really-quiet -loop 0 ~/.scripts/silence-10sec.mp3 &
elif [ "$event" == "$unplugged" ]
then
pkill -f mplayer
fi
done
Make it executable with the command chmod +755 silence.sh
Step #4: Type startup applications
in the Dash and add the path of silence.sh
script to it.

So, basically what we are doing is listening to events of headphones being connected and disconnected and playing or stopping the blank mp3 file in a loop. Now, I have used mplayer
because I know that I am never gonna use that for any other purpose and killing its process is completely safe for me.
I came up with this idea when Serg answered one of my questions.
He came up with this script and I just made modifications to it to suit the requirement.