There's a diagnostic tool accessible from the Phone app that can be used to perform different types of hardware tests, such as audio test which checks both the microphone and speaker, backlight test, and more.
Reboot into Safe Mode:
- Android® 7 and later: Press and hold the button, then tap and hold Power off until the phone reboots.
- Android 6: Press and hold the button, then tap and hold Power off until the Reboot to safe mode prompt appears. Tap OK.
- After the phone reboots, open the Phone app and dial *#*#3424#*#*.
- If prompted, tap Accept.
- Select one or more types of test to perform, and then tap Run.
- Follow the instructions to test the speakers and microphone.
- Reboot the phone to exit Safe Mode.