I've been wondering this for a while. If you talk to some people and ask them whether they often have music playing in their head, some of them will say yes. I know this is especially true for people with ADHD, and likely for people with other neurodevelopmental disorders too.
Before the invention of recording music, music was always tied to places, people and instruments. If you heard music, you could walk up to the source and point out the people playing their instruments. A violin shaped object plays violin-like sounds, pianos play piano-like sounds, etc.
Nowadays, music can play from almost anything anywhere. In the case of speakers, there's at least a source you can point to, but the music has lost all of its context. No matter if the music you're listening is a recording of a band, an orchestra or just an individual playing the piano, it's all coming from the same device.
Headphones and earphones lose even more context, any music can be played anywhere, and it sounds as if it's coming straight from within you. Outside of concerts and such, music is much less tied to people, instruments, venues, and other such contexts.
Because of this, I hypothesised at some point that music getting stuck in the head might be caused by the fact that music is available anywhere at any time.
Because of this, I am very curious whether this phenomenon has gotten more prevalent in modern times were listening to all kinds of music in practically any situation or location is viable.
Are there any/many accounts of music getting stuck in one's head pre the invention of music recording?
It's a difficult question to answer in some ways, because the English word 'earworm' is really only 15 years old (a translation of sorts from a German word in an Oliver Sacks book), and so using it as a search term isn't going to get you very far. And, generally, most of our surviving writing from, basically, any time before the modern period is official documents - not the kind of thing where people would casually mention earworms. So there is a possibility that it is a modern phenomenon and a possibility that Caesar had a song by that damned bard Cacofonix stuck in his head. In a 2018 paper, Philip Beaman does point out that the appearance of earworms 'predates the modern scientific interest by some considerable time'. The earliest example he can find in English definitely does predate radio - he points out a short story by Edgar Allen Poe called 'The Imp Of The Perverse', where Poe's character says:
I could scarcely get rid of it for an instant. It is quite a common thing to be thus annoyed with the ringing in our ears, or rather in our memories, of the burthen of some ordinary song, or some unimpressive snatches from an opera. Nor will we be the less tormented if the song in itself be good, or the opera air meritorious.
Poe wrote this in 1845, a good 30 years or so before Marconi was born.
Mark Twain also mentions an earworm in a 1876 short story, 'A Literary Nightmare':
my feet were keeping time to that relentless jingle. When I could stand it no longer I altered my step. But it did no good; those rhymes accommodated themselves to the new step and went on harassing me just as before.
So clearly, American writers of the 19th century were capable of getting songs stuck in their head, despite the lack of the radio - one thing to remember about music in a pre-radio era was that just because music was not available over the airwaves doesn't mean that it wasn't ubiquitous to some extent - the rise of radio has probably meant that people's at-home or at-work musicality and singing has declined, and people still would have had favourite songs they sang around the piano, mothers still would have sung lullabyes, teachers would have still led young kids in singing. And - as the Poe quote illustrates - people went to live performances of opera and came out with songs stuck in their head.
Modern scientific research on earworms suggests that repetition of the music makes it more likely to be stuck in your head. People in the era of radio or more recently streaming services have more opportunity to hear the same song over and over again than Edgar Allen Poe did (presumably it would cost quite a lot of money to have your own personal musician play music for you all day while you worked), and its probably the case that modern pop is much more carefully put together to get stuck in heads than, say, a Stephen Foster song from the 1840s - potentially the kind of thing Poe might have been thinking about as an 'ordinary song' ('Oh Susanna' is from 1848, says wiki, 'Camptown Races' is 1850)
...my apologies if you end up with a pretty blatantly racist blackface minstrel song like 'Camptown Races' stuck in your head. Sorry about that.
I should also say that the 2018 paper by Philip Beaman I've based these examples on is definitely a psychology paper rather than a history paper, and I think it's the only academic research into the history of the phenomenon. I rather suspect that a proper historian with decent research skills (and/or who is bilingual) could potentially discover pre-modern examples of earworms being discussed. I mean, perhaps Cicero does discuss an earworm in his letters or a character in an Aristophanes play is tormented by them. Or perhaps some medieval monk complains about having 'Sumer Is Icumen In' stuck in their head. Perhaps the earworm does (unlike Poe) predate the modern capitalist music industry (which made a bunch of money in Poe's day selling sheet music, and where there were of course opera houses and so forth where people paid to hear music) and not just the radio. We may find out!