You could have damaged the solder joints on the cable. You can replace the the earpiece speaker. But my guess is a bad connection to the motherboard. Remove that connector and make sure all of the pins are straight and clean the connector with alcohol and a qtip and a soft brush. I actually had the same problem with xs max and it was the connection.
+
+
=== Update (06/08/2020) ===
+
Yes that fixed the issue. I would first check the solder joints on the cable and earpiece speaker and then check the connection to the motherboard and make sure the pins aren't damaged and clean it and hopefully that will solve your problems.
You could have damaged the solder joints on the cable. You can replace the the earpiece speaker. But my guess is a bad connection to the motherboard. Remove that connector and make sure all of the pins are straight and clean the connector with alcohol and a qtip and a soft brush. I actually had the same problem with xs max and it was the connection.