OK. Lets stick on your first code that looks OK to me.

What is connected to your buzzer output? Can you connect only a LED with a resistor for testing purposes?

And I would also consider having decoupling capacitors the power rails, as close as possible to the PIC power pins.

Maybe you have an interference of the buzzer if it is electromagnetic?

Ioannis