Option 1...
The simplest fix is to restart the device. This can sometimes, not always, eliminate any glitches.
Option 2...
The silent/vibrate switch may have been turned on. Here are two ways to check that...
A. To check this you can either use the drop down menu (simply slide your finger from the top of the screen down.). Here you will find options for a few different things like "Wi-Fi", "Screen Brightness", etc. Choose the speaker option and make sure the slider is all the way up.
B. Another way to do this is to go into the "Settings", then "Sound", and finally "Volume". From this screen you can adjust all volumes and make sure that none of them are set to low or on vibrate.
Option 3...
Make sure your phone has the most recent updates. If any changes have been made to the OS for your device, it will have included fixes for known issues. Which your problem could be one.
Option 4...
Your phone is not switching to headset from speaker. This can happen if there is any damage in the jack, or damage to the headset/earbuds.