Speaker sound no longer works on iPhone 3GS?

Suddenly a few days ago my iPhone sound stopped working and I have no idea why or how to fix it. My telephone loudspeaker doesn't work, my phone doesn't ring, no keyboard clicks and no noises from apps and notifications. The sound works when I have earphones in but not when on speaker. Yes I have the sound on my phone switched on and the ringer volume is at its maximum. The weird thing is that the sound works when I have the charger cable in and put pressure on it. Basically this means to get the sound to work I have to put the charger in and then wrap the cord around my phone so it's tight.
I got my battery replaced recently so thought it would be because of that but unlikely as the sound died a week later.
Any ideas?

I think your iphone get software problem because last time i have also get same kind of problem then i asked to mobile repairing person then he told me it is software problem then i did software then it become normal so i hope that you will follow my instructions.