I want to drive a speaker, in series with a 10uF electrolytic capacitor, from a microcontroller and was wondering what will happen when the GPIO pin goes low, given that the capacitor will try to discharge. Is it safe ? Or do I need to create an alternative path to ground with a resistor ?
No it is not safe. The MCU is going to malfunction at the long run.
As an example, for AVR microcontrollers sink current must not be bigger than 20ma. So you have to make sure that the discharging current will not go higher.
I would recommend to connect the speaker through a driver which could be a transistor or an amplifier.
With the MCU I/O pin configured as an output, it will use a push-pull ('totem pole') driver. This consists of a FET from the chip's Vdd supply to the I/O pin and another FET from the I/O pin to the chip's GND supply.
When the pin is driving high, it sources (outputs) a current to Vdd and when driving low, it sinks (inputs) a current to GND. These currents should be stated in the datasheet, usually as IoH(max) and IoL(max) respectively. If the load exceeds these specified currents, the FETs will limit the current flow and dissipate power based on the voltage drop across the particular FET and the current drawn. If maintained for long enough, this could damage the MCU.
However, with a switching action your audio experiment/application will require, potential damage is unlikely to be a problem. You may not get the behaviour you hope for but it will be fine to experiment with.
Majid_L makes sense - a buffer stage will stress your microcontroller's I/O pin less. An 8-ohm speaker will ask for current above permissible limits at times (for moments after switching high or switching low) when driven directly. A buffer can be simple, like this:
simulate this circuit – Schematic created using CircuitLab
With the transistor buffer, the I/O pin will see about 800 ohm load, instead of 8 ohms (assuming transistor current gain is 100). The transistor type is not critical, but the bottom one must be PNP, top one NPN.
C1 is added because lots of pulse current is pulled from the 5V supply by the transistors. You don't want these pulses to reduce the microcontroller's Vdd, else erratic operation (or brownout) may occur.