To build off of StoneyB's excellent answer in the linked question, this is an example of context changing the difference between "by myself" and "on my own". Here, "I didn't hear this song on my own" suggests that the speaker received assistance or support from someone else, which sounds strange since hearing is normally an action that doesn't require assistance from another person. Thus, "I didn't hear this song when I was by myself" is correct (since you are trying to say that the speaker wasn't alone when they heard the song) and the second sentence is incorrect.
To provide contrast for this answer, say that you had the following sentences:
I didn't write this song when I was by myself.
I didn't write this song on my own.
In this case, "writing a song" is an action which can (and often does!) involve people working together, so the second sentence is now grammatically correct.