On most phones, Usually with two gold plated springs/legs that touch onto two pads on the logic board. Clean the pads with 99% Isopropyl alcohol, only if they look dark or corroded. Also clean and slightly (not a lot) pull out the springs so they stick out slightly further making better contact.
Replace the adhesive so it doesn’t move out of place. That usually fixes it. Failing that replace the speaker.