No1 check is to clean out headphone jack of dirt and fluff. It sometimes tricks the phone into thinking headphones are in!
No2 check settings general sounds
No3 press and hold power and home for 10 sec until apple appears then allow to reboot
No4 replace dock or mic/speaker flex on dock( replacing entire dock is easier)