Guys I think you have to Download soundabout app in your phone it work for me. app's link
https://play.google.com/store/apps/details?id=com.woodslink.android.wiredheadphoneroutingfix&hl=en
Open this app and select''' media audio''' option tick on speaker if you have speaker problem. And if you have earpiece problem then select '''phone call audio''' option and tick on earpiece.