This is likely to be a subject where the answer varies by jurisdiction.
In the USA, there are some limits for pilots flying commercial airliners in scheduled airline passenger service (i.e. Part 121 operations.) In particular, most of the types of devices that would be used to play said music are banned for use by pilots during all phases of flight in Part 121 operations.
14 CFR 121.542(d) says the following:
(d) During all flight time as defined in 14 CFR 1.1, no flight crewmember may use, nor may any pilot in command permit the use of, a personal wireless communications device (as defined in 49 U.S.C. 44732(d)) or laptop computer while at a flight crewmember duty station unless the purpose is directly related to operation of the aircraft, or for emergency, safety-related, or employment-related communications, in accordance with air carrier procedures approved by the Administrator.
49 U.S.C. 44732(d) defines "personal wireless communications device" as:
(d) Personal Wireless Communications Device Defined.—
In this section, the term “personal wireless communications device” means a device through which personal wireless services (as defined in section 332(c)(7)(C)(i) of the Communications Act of 1934 (47 U.S.C. 332(c)(7)(C)(i))) are transmitted.
This particular question of being able to play music came up when this regulation was added back in 2014. The FAA had this to say about it in their final rule publication in the Federal Register (emphasis mine):
Several commenters asked if the limitations in the rule extended to specific devices, such as iPods, used to listen to music. As stated in the NPRM, Section 307 of the Act defines “personal wireless communications device” as a device through which personal wireless services (as defined in Section 332(c)(7)(C)(i) of the Communications Act of 1934) are transmitted. The Communications Act of 1934 states that personal wireless services means commercial mobile services, unlicensed wireless services, and common carrier wireless exchange access service.
In general, wireless telecommunications is the transfer of information between two or more points that are not physically connected. In the final rule, the FAA retains the same broad category of included devices because a list of specific devices would ignore the reality of evolving technology. This broad category includes, but is not limited to, devices such as cell phones, smartphones, personal digital assistants, tablets, e-readers, some (but not all) gaming systems, iPods and MP3 players, as well as netbooks and notebook computers.
Evolving technology makes it difficult to develop an inclusive list of devices that are addressed by the provisions of the final rule. The FAA notes that the final rule establishes a clear definition of personal wireless communications devices. The provisions of the final rule do not prohibit the use of devices that do not meet the definition of personal wireless communications devices.
So, while playing music isn't specifically banned, most of the devices that would be used to do so are in Part 121 operations. Part 121 operations are those operated by U.S.-flagged scheduled air carriers. It does appear that a Part 121 pilot could still play music (during non-critical flight phases) using an MP3 player that had no wireless communications functionality (i.e. no Wi-Fi, Bluetooth, or cellular radio.)
As far as the 'why' part of the question, this is also addressed by the final rule publication in the Federal Register:
B. Statement of the Problem
Several incidents involving a breakdown of cockpit discipline prompted Congress to address this issue via legislation. In one instance, two pilots were using their personal laptop computers during cruise flight and lost situational awareness, leading to a 150 mile fly-by of their destination. In another instance, a pilot sent a text message on her personal cell phone during the taxi phase of the flight after the aircraft pushed back from the gate and before the take-off sequence. These incidents illustrate the potential for such devices to create a hazardous distraction during critical phases of flight.
This rule will ensure that certain non-essential activities do not contribute to the challenge of task management on the flight deck and do not contribute to a loss of situational awareness due to attention to non-essential activities, as highlighted by these incidents. See 78 FR 2912 (Jan. 15, 2013).
C. National Transportation Safety Board Recommendations
In its recommendations to the FAA regarding the Colgan accident in 2009, the National Transportation Safety Board (NTSB) concluded that because of the continuing number of accidents involving a breakdown in sterile cockpit discipline, collaborative action by the FAA and the aviation industry to address this issue was warranted. Therefore, the NTSB recommended (A-10-30) that the FAA require all part 121, 135, and 91 subpart K operators to incorporate explicit guidance to pilots, including checklist reminders as appropriate, prohibiting the use of personal portable electronic devices on the flight deck.
The document then goes on to describe another incident that motivated the NTSB recommendation where a medevac helicopter crashed killing the pilot, nurse, paramedic, and patient. The helicopter's engine failed due to fuel exhaustion due to the pilot not ensuring that the helicopter was adequately fueled prior to departure. "The investigation determined that the pilot engaged in frequent personal texting, both before and during the accident flight."
So, in short, all use of personal wireless communications devices in Part 121 operations was banned due in order to prevent dangerous crew distraction from texting and such.