A few years ago, the headphone jack on my iPod Touch became unreliable. The sound was often muffled or distorted, and twisting the plug would sometimes restore it. I contacted Apple about it and after asking some standard troubleshooting questions, they replaced the iPod. A while later the same problem happened with the new iPod – now out of warranty. Luckily I found a fix, and it seems that Apple could have saved themselves some money if they’d known to suggest I try this before replacing my original iPod.
The problem was simply lint from my pockets that gradually accumulated inside the headphone jack.
The solution was simply to reach in with a wooden toothpick and fish out the lint. Since first doing this, I’ve done it several times. (A plastic toothpick should work find too. However, I would NOT poke anything metal, such as an unbent paperclip, into an electronic device, since it might cause a short circuit.)