To listen to music specifically when walking is harmful in one case: you can cross the roadway and simply not hear the car honking. Such cases are a dime a dozen, just like at a railway crossing.
Pulling bananas from your ears for 10 seconds can save yourself 70-80 years
Listening to music with headphones is generally harmful. Even lying down. And in general, listening to music is harmful. As well as breathing.
I once broke my nose on the street when they tried to gop. All because I was walking in headphones and did not hear the polite request for a light.