(iOS) getUserMedia() cause wrong audio output

In my web app, an MP3 is playing through the speaker.
When I call getUserMedia() API to capture audio, the audio will correctly switcher to the earpiece — which is expected.
However, after releasing the MediaStream(stop the audio track), this MP3 continues to play through the earpiece instead of switching back to the speaker.

Environment: iPhone 15 Pro Max (iOS 18.5, Safari)
Reference: WebKit Bug #295889

Reproduce the bug

  1. Play the Music
  2. Click the Request Audio Stream button(The Stream will release automatically)
  3. The audio will be output to the earpiece and not discover the speaker