First use the service menu to test the speaker,
open the phone and type *#*#7378423#*#*
select service tests - stereo speaker right & stereo speaker left.
If the speaker is working then it's a software fault.
If it's faulty then you should try and get it fixed professionally or find a strip down on ifixit and do it yourself