14

I tried using AVAudioSession and AVAudioPlayer to record and play sounds respectively but the sound volume is very low.

I tried putting volume value of AVAudioPlayer to 1.0 and more but didn't help much.

What could be my other options to record sound which can be loud enough to play back?

halfer
  • 19,824
  • 17
  • 99
  • 186
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • Are you using the built-in microphone, or the mic that's on the cord of the standard earbuds? How close is the source of the sound to the mic? What kind of values are you seeing if you log -peakPowerForChannel? – NSResponder Apr 14 '11 at 11:14
  • @NSResponder: I am using built-in microphone. Also can you explain how to log -peakPowerForChannel? – Parth Bhatt Apr 14 '11 at 11:16

6 Answers6

25

This code should be useful for you:

#import <AudioToolbox/AudioServices.h>

    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;                
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,          
                                 sizeof (audioRouteOverride),&audioRouteOverride);  

It will increase volume. The functionality of the code is to convert the ordinary sound to speaker sound on ur iPhone. That's why kAudioSessionOverrideAudioRoute_Speaker is used.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
CNSivakumar
  • 1,043
  • 2
  • 10
  • 13
23

Since iOS7 you can fix this issue directly with AVAudioSession

The overrideOutputAudioPort method does the same than AudioSessionSetProperty

NSError *setOverrideError;
NSError *setCategoryError;

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];

if(setCategoryError){
    NSLog(@"%@", [setCategoryError description]);
}

[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&setOverrideError];


if(setOverrideError){
    NSLog(@"%@", [setOverrideError description]);
}
Martin Christmann
  • 2,488
  • 2
  • 21
  • 17
  • this line saved my life, thanks man [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&setOverrideError]; – Fadi Abuzant Aug 16 '17 at 08:40
4

Swift version :

 import AVFoundation
 var overrideError : NSError?
    if AVAudioSession.sharedInstance().overrideOutputAudioPort(.Speaker, error: &error){

    }else{
        print("error in overrideOutputAudioPort " + overrideError!.localizedDescription)
    }

Swift 2:

        do {
        try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)
        } catch {
        }
M.Othman
  • 5,132
  • 3
  • 35
  • 39
1

The following code fixed this issue for me:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
UInt32 doChangeDefault = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefault), &doChangeDefault);
Groot
  • 13,943
  • 6
  • 61
  • 72
Kemo
  • 488
  • 6
  • 12
0

MAN, try this.

AVAudioSession *session = [AVAudioSession sharedInstance];

NSError *setCategoryError = nil;
if (![session setCategory:AVAudioSessionCategoryPlayback
              withOptions:kAudioSessionOverrideAudioRoute_Speaker
                    error:&setCategoryError]) {
        // handle error
}

NSError *error;
NSData * data = [NSData dataWithContentsOfURL:self.playlet.urlSound];

self.audioPlayer = [[AVAudioPlayer alloc] initWithData:data fileTypeHint:@"aac" error:&error];

self.audioPlayer.delegate = self;

if (error)
    NSLog(@"Error: %@",
          [error localizedDescription]);
else
    [_audioPlayer play];
0

just set this [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&setOverrideError]; It works for me.

Chauyan
  • 157
  • 8