Sound processing - headphones vs. phone speaker

Hello

In the mobile phone game I'm working on, I've built a music-making sequencer of sorts. The way it works is that 5 separate audio tracks (3 instruments with notes and 2 'drummers') are being played simultaneously, 'going to' different notes at different moments, and this way I can program it to play any music I want.

The sound quality is great when I test export this app to Windows (the game is being made in GameMaker on Windows).

Running the app on iOS, the sound is also great, but... only with headphones plugged in. There's this 'click' sound at the beginning of each note when playing through the phone speaker.

To be more specific, what actually happens is that when I start the app with sound going via the speaker - this distortion occurs. Then when I plug in the headphones while the app is running, the distortion is still there. BUT if I start the app with headphones plugged in, it sounds correct, AND THEN when I pull out the headphones while the app is running it still sounds correct going through the speaker.

It almost seems like the sound is processed differently by the OS when the app is started 'in headphones mode' and different in 'speaker mode' - is that possible?

What's causing this?

I'm testing on an iPhone 6 btw.