最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - iOS Safari lowers audio playback volume when mic is in use - Stack Overflow

programmeradmin1浏览0评论

I have been trying to record and play an audio response ing 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?

I have been trying to record and play an audio response ing 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?

Share Improve this question asked Apr 23, 2023 at 8:02 Shyam3089Shyam3089 4992 gold badges7 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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.

This can be caused by echo cancellation. This feature tries to prevent the the audio playback from being picked up by the microphone.

This feature can be turned off programmatically, e.g.

const stream = await navigator.mediaDevices.getUserMedia({ 
  audio: { echoCancellation: false },
});

This requests a "track" with echo cancellation turned off. The system can satisfy this request by supplying a device that would otherwise have echo cancellation turned on by default

发布评论

评论列表(0)

  1. 暂无评论