I assume you’ve tried different headphones? It definitely sounds like a loose connection. You can try cleaning the port out with compressed air and a tooth pick with some rubbing alcohol(dipped on the tooth pick)
if it’s not due to dust,it’s probably a bad port. It would require a replacement. This means disassembling the phone. Sometimes it is soldered on the board,other times it is a separate part that plugs into the board.
You can also get Bluetooth earbuds if the above is too much of a hassle.