I was wondering that if a pilot's flight had been delayed or a pilot had nothing to do, would it be possible for a pilot to play music in the cockpit?
With the proper equipment, it is possible.
For example on the Garmin GTN 750. Music is listed as a service on this avionic.
And you can even create multiple music channels and assign it to different people on the intercom, of course with mute feature when someone speaks either on the intercom or radio.
Depending on the equipment in the aircraft, the answer may very well be yes. A lot of modern headsets in civilian aircraft (particularly those with active noise reduction) have bluetooth capability that allows music streaming. Some more modern audio panels even have music inputs now. I even flew in an Archer that had a CD player on the panel and wired into the audio panel.
If you are lucky enough to have an automatic direction finder (ADF) eating up useful load in your aircraft you may be able to tune into AM radio stations. This may have contributed to the crash of Varig Flight 254.
While you are legally allowed to do this (at least in small planes) the sterile cockpit rules that most if not all air carriers have prevent professional pilots from doing so, as @Carlo Felicione points out in the comments