7

I am developing an iOS app wherein I should be able to record a sound, save it to the filesystem and then play it back from the disk. When I try to play the file off the disk, I could barely hear anything. The volume is too low. But I'm pretty sure I'd set the device volume to max. I am using AVAudioRecorder and AVAudioPlayer` for recording and playing.

Could someone please point out what the issue could be?

stack2012
  • 2,146
  • 2
  • 16
  • 23

4 Answers4

12

For most the solution here will probably be to use the .defaultToSpeaker option with the AVAudioSessionCategoryPlayAndRecord category:

try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker])

But for me the issue turned out to be that a different component in my app was setting the AVAudioSessionModeMeasurement mode on the audio session, which for whatever reason reduces the output volume.

John Scalo
  • 3,194
  • 1
  • 27
  • 35
  • 2
    swift 5: try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth, .defaultToSpeaker]) – Ning Jul 30 '20 at 01:38
2

Set category of AVAudioSession using this method

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
Mehsam Saeed
  • 235
  • 2
  • 14
0

try this code. I think setMode fixes my issue.

NSError * outError = nil;
AVAudioSession * session = [AVAudioSession sharedInstance];
[session setActive:NO error:nil];
[ session overrideOutputAudioPort: AVAudioSessionPortOverrideSpeaker error:&outError];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setMode:AVAudioSessionModeSpokenAudio error:nil];
[session setActive:YES error:nil];
if(outError){
    NSLog(@"speak error %@", outError);
}
John-L
  • 21
  • 1
  • 2
0

I've been dabbling with this problem as well and tried a bunch of different solutions without luck until I finally tried this:

    let session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(.playAndRecord, mode: .default)
        try session.setActive(true, options: .notifyOthersOnDeactivation)
        try session.overrideOutputAudioPort(.speaker)
    } catch {
        debugPrint("error: \(error.localizedDescription)")
    }

and this seems to have switched the playback to actual speaker. Setting the category mode to .default seems to do the trick. Even overrideOutputAudioPort(.speaker) was not actually doing it once it got stuck in the phone call speaker output.

C0D3
  • 6,440
  • 8
  • 43
  • 67