Transistor Power Dissipation / Overheating
You have a 4 Ω speaker and 0.4 A. The power dissipated in the speaker is given by \$ P = I^2R = 0.4^2 \times 4 = 0.64 \ \text W \$ so your calculation is rather a bit off.

The voltage across the speaker is \$ V = IR = 0.4 * 4 = 1.6 \ \text V \$.

You have a 12 V supply so that means that the voltage across the transistor when on = 12 - 1.6 = 10.4 V. At 0.4 A that means the power dissipated in the transistor is \$ P = VI = 10.4 \times 0.4 = 4.16 \ \text W \$.

Note that both of these calculations are for when the transistor is on. When it is off (50% of the time for a squarewave) the current is zero so the power in the speaker and transistor will be zero also. This will result in an average power of half of the calculations above.


  If I ... added another current limiting method, such as a power resistor in series with the speaker, would that reduce the amount of heat dissipated by the transistor?


Yes. You can afford to drop 6 to 8 V there at 0.4 A. You can work it out.



From the comments:


  ... but the voltage across the speaker is 12V, and current is 0.4A. 


If that were true then the speaker resistance must be \$ R = \frac {V}{I} = \frac {12}{0.4} = 30 \ \Omega\$. Try measuring the voltage across the speaker terminals. It should be close to 1.6 V as calculated above.


  So also by ohm's law we can arrive at the conclusion that: P = V x I = 0.4 x 12 = 4.8W. 


That's the power for the whole circuit. 4.16 W of that is lost in the transistor.


  I guess I'm just confused as to which applies in this case, when the transistor is acting as a resistor.


Your understanding is correct that the transistor is behaving like a variable resistor in that it limits the current.

Let's look at the current gain and do a little more maths. 



Figure 1. DC current gain from the TIP31/32 datasheet.


Your 10k base resistor will give a base current of \$ \frac {12}{10k} = 1.2\ \text {mA}\$.
We're not using the 4 V of the datasheet (we're using 12 V) but let's assume that the hFE is between 25 and 50. That means that a base current of 1.2 mA should give us a collector current of 30 to 60 mA. Lo and behold, you measured 0.4 A.


Note that this is not a good way to set the speaker current if multiple such circuits were being built as the collector current would vary significantly from transistor to transistor.