iPhone making no noise except ringtones
It's clear that it's not a hardware issues since the sound works in certain scenarios. It seems to be a software based issue. I would suggest backing up and restoring the device using these steps. Make sure you set it up as a new device so as not to transfer any old software back to the phone after the restore.