When listening to music with the ear buds, a lot of the times it will switch between the earphone and the speaker phone. Its very annoying. I hope somebody can tell me whats going on with it.
If your headset fitting is not secure in the headset port, the device may think you've unplugged the headset from the device. This would explain why it is switching back and forth.
I expect this is a manufacturer defect in the port, a result of damage to the device headset port, or a poorly manufactured headset.
I would start by testing it with another headset in the port to see if it happens with every headset, or just yours.
If the headset is just loose and you want to keep the headset, maybe a converter will fit more tightly. Here's a sample pic...
It seems to happen with any headset that I use. They feel tight(sometimes I think it feels to tight, but I know thats not the case) and secure when when I push them in, not loose at all.