Moto G2 speaker phone issue when calling My Motorola 2nd Generation(XT1068) mobile running Lollipop 5.0.1 version is facing problems after removing headphone. While I can talk using the headphones' mic and the phone mic has no problems and the other person can hear what I speaking. But I could not hear the other person's voice from the earpiece or when loud speaker mode is on. If I press the loudspeaker, the icon loudspeaker is shown at the notification bar but I cant hear a thing. So everytime I need to use the headset to hear the other person's voice and its annoying. 

I cleared the cache and data of the Dialer app but din't help. The only temporary solution that works is to restart the phone. After restarting the phone, the earpiece works fine and I am able to talk and hear properly. But again if I insert and remove the headset to listen to music the earpiece stop working and I couldn't hear the other persons voice on a phone call. I have also tried inserting alternative headsets. I've also tried removing the headsets slowly that din't help either.

I thought the phone was still in the headset mode but if I play song or video from Gallery/Media player or Google Play music or through File Manager apps the two speakers in the front work fine. And If I remove the headset the songs playing in the Play music pauses which means there is no problem in the headset and jack.

Any ideas on how to solve this issue? Could this be a hardware or a software issue?

Update 1:

Troubleshoooting methods suggested by Motorola Support:


Clearing Cache, Data of Dialer and Phone apps.
Wipe Cache Partition on recovery mode.


However both of the above didn't solve my problem. So they suggested me to do a factory reset which I don't want to do since factory resetting will erase all my data.

Update 2:

There was also another problem which gave me "Unable to play this file" in the Play Music app while playing a song. If I open the Dialer app when a song in playing in background(in Play Music/other music player apps) the song stops on it own and wont play and the Play button won't work until I change to next track or sometimes I need to restart the Music player app.
Solution:
Factory reset.
Finally, after doing all the necessary backups using different tools like adb, Helium(Carbon), Google, Super Backup,  I did a factory reset(Soft Reset through the option under Settings &gt; Backup &amp; Restore &gt; Factory Reset) and the speaker phone issue is solved.