I believe the browser stops the sound as part of the page loading process. Therefore, by the time your custom upload function was called, the sound was stopped.
You can use onbeforeunload instead of onunload - this may work (unverified).
Alternatively, you can set a flag when the sound starts and use the onended event to change this flag when the sound ends, or when the user manually stops or pauses it.
Dan blows
source share