WWI, actually. It was first played at the 7th inning stretch of a world series game. It got a very favorable reaction, so later in the series a band was brought in and played it before the game. For awhile it was only played at special events, but became a regular thing during WWII and it stayed on and spread to other sporting events.
I may get blasted for this, but I sympathize with the players. It gets played a lot before running races. For a competitive runner, you have a way to get ready for races, which usually includes getting your heart rate up so that when the race starts you aren't going from resting to high. Standing still for two minutes does not work well with that plan. I admit that I'm a swayer, to try to stay loose and keep my heart rate up a bit. I've also got a mindset for the race, and I'm not going to interrupt that to think about what the star spangled banner means to me, especially as it gets played so often I'm kind of immune to it. And I'm nowhere near a pro or college level athlete. These athletes have an even more structured approach and a lot, lot more on the line than my little races. And baseball players hear it 162 times or more during the season.
Put it this way, suppose you're about to go into a huge sales pitch to a customer, or about to give a major presentation. You're all set and keyed up, but then everything stops for 2 minutes to sing the anthem. Are you really focusing on the anthem, or thinking ahead for the major task you are about to do?
I'm not going to go as far as say it shouldn't be played anymore. Nobody is going to take on the bad PR for being so unpatriotic as to stop playing the anthem. But I'm cutting the players some slack. A lot of slack. I'd see nothing wrong with playing it while the players are in the locker room.