Search code examples
javascriptiossafarimobile-safariweb-audio-api

iOS Safari lowers audio playback volume when mic is in use


I have been trying to record and play an audio response coming from the server using web audio API. But when mic is in use or even stopped programmatically the playback volume is very low. It seems a feature of iPhone but it's really annoying when you are running a voice assistant like app in the browser. Here's what I tried using javascript:

stream.getTracks().forEach(track => track.stop()) // Mic muted but still volume is low

and

stream.forEach(function(track) {
    track.enabled = false; // Mic muted but still volume is low

});

Revoking mic permission works but it will be annoying when asking permission every time user trying to record. Is there any other efficient way to temporarily stop mic or regain the original playback volume?


Solution

  • Audio Session API

    ...probably is the best solution for you.

    There are several audio session types for different purposes. Default value is auto and in your use case you probably want to set it to play-and-record, as it allows you to use both your mic and your speaker:

    navigator.audioSession.type = 'play-and-record';
    

    You can read it further here: https://w3c.github.io/audio-session/

    Note: at the time of writing - Audio Session API is in an "Editor’s Draft" state, so you should check for that particular navigator property prior to rely on it. But it does work in Safari on iOS 16.4.1, though.