I have a use case where I've to play speech utterances and audio files in an ongoing video call. I'm using Agora for the video call.
Issue:- The sound of speech utterance and audio file playing is very low
Observations:-
- While in video call the voice of the participants is fine and coming from both speakers of the iPhone(i.e, no decrease in volume)
- During the call AVAudioSession has following properties:
- While playing speech utterances or audio files(using AVAudioPlayer) voice is coming from both speakers: earpiece and bottom BUT the volume are very low.
- Tried connecting my phone to a Bluetooth speaker the issue is still there.
- Tried testing at different battery levels in my iPhone, the issue persists.
- This low volume issue is not 100% recurring i.e, in approx 20% of cases it works fine.
my code for Speech utterance: -
let speechUtterance = AVSpeechUtterance(string: "Some text")
speechUtterance.rate = 0.4
speechUtterance.volume = 1.0
speechUtterance.voice = AVSpeechSynthesisVoice(identifier: "com.apple.ttsbundle.Samantha-compact")
speechSynthesizer.speak(speechUtterance)
my code for playing audio files:-
private func playGameSound(ofType sound: GameSounds) {
if let path = Bundle.main.path(forResource: sound.rawValue, ofType: nil) {
let url = URL(fileURLWithPath: path)
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.delegate = self
soundsPlayingCount += 1
audioPlayer?.play()
} catch {
print("couldn't load game sound file")
}
}
}
PS:- I've seen multiple stack answers that suggest the following code:-
AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
I've tried this but it does not help.
Moreover, I want to know why in the first place this low-volume behavior happens and that too not always. Also, please suggest to me some resources that might guide me towards the solution to this problem.