The phone uses different speakers when you're talking on the phone vs. on speakerphone. In normal use the sound comes through the earpiece speaker, while on speakerphone it uses the lower speaker at the bottom of the phone. As you've found, the earpiece speaker doesn't seem to be working, but the lower speaker is fine.
First thing to do is to clean the speaker mesh. A common problem is to get pocket lint, dust or dirt packed into the speaker opening, blocking the sound from coming out. There are a lot of different opinions on the best way to do that; personally I'd start with a fine brush and some 90% or higher concentration isopropyl (rubbing) alcohol. Vacuum and canned air are also methods that have worked for various people as well.
If that doesn't help, then it's possible your speaker has failed. The bad news here is that the assembly that the speaker is a part of includes the flood illuminator, part of the Face ID system. That part is paired to the motherboard so replacing it yourself will result in the loss of Face ID. If that's acceptable, then you can use the Earpiece Speaker Assembly replacement guide to install a new speaker and hopefully that will fix the speaker problem, at the cost of losing Face ID.
iPhone 12 Earpiece Speaker and Front Sensor Assembly Replacement - iFixit Repair Guide
You can save Face ID by either having the screen replaced by an Apple authorized service center (their replacement screen comes with the earpiece speaker assembly) or using the self repair program to do it yourself. It will save you a small amount of money, but you still have to buy the screen from Apple and use their tools to do the work.
However, there is a third option if you can do a very small amount of fine soldering. The speaker itself is only connected to the flex cable assembly by two solder points, so it is entirely possible to replace just the speaker itself, keeping the rest of the assembly and thereby retaining Face ID. I wrote a guide to that for the iPhone X, but the procedure is virtually identical for the 12. Here's a link to my iPhone X guide.
iPhone X Earpiece Speaker Replacement Without Losing Face ID - iFixit Repair Guide
I can step you through exactly how to do this. First, follow the iPhone 12 guide up through Step 28. Once you have the speaker flipped over, you'll be able to see the solder points you have to unsolder. Here they are marked in red.
Now switch to the iPhone X guide and go to Step 29. Unsolder the old speaker and solder in a new replacement part. Now you can go back to the iPhone 12 guide and follow the instructions in reverse order to put it all back together. Although iFixit doesn't have a guide to that particular repair, they actually do sell the part you need; just the speaker without the rest of the front sensor assembly, which is exactly what you would need to do this particular repair.
iPhone 12/12 Pro Earpiece Speaker
Hopefully that gives you everything you need to know. If you have any questions feel free to ask, otherwise good luck and be sure to come on back and let us know how it all turns out; you never know, it could give the next guy with the same problem the exact answer they need.
crwdns2934105:0crwdne2934105:0
crwdns2934113:0crwdne2934113:0
crwdns2915270:0crwdne2915270:0
crwdns2889612:0crwdne2889612:0
2