32
votes

I'm recording audio with AVAudioRecorder as seen in How do I record audio on iPhone with AVAudioRecorder?

I then use AVAudioPlayer to play back the recording. However the sound is coming out of the ear speaker, not the loud speaker. How would I go about redirecting the sound to the loud speaker ?

TIA!

11

11 Answers

31
votes

I realize this question is fairly old but when I was struggling with the same problem I found a simple solution that hopefully will help anyone else looking to use the louder media speakers as opposed to the receiver speakers. The method I used was setting up the audio session with the DefaultToSpeaker option in AVAudioSessionCategoryOptions:

In Swift (assuming your audio session is named session) -

session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker, error: nil)

In Obj-C -

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
18
votes

From http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/12890-audiosessionsetproperty-problem-playing-sound-listening-mic.html --

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);    
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
17
votes

Swift 3

Before recording the sound I set:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)

and before playback I set:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

Objective C

before recording:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error:nil];

before playback:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
13
votes

This works great for me in Swift2

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker)
6
votes

This is an old question, but the other answer did not help me... However, I found a solution which I am posting for future reference in case someone (or myself from the future!) needs it.

The solution is described in the following blog post: iOS: Force audio output to speakers while headphones are plugged in .

You need to create new Objective-C class AudioRouter in your project. Then import AudioRouter.h into your header file of the class where you are initiating audio functionality. Next, in the corresponding .m file add the following lines within viewDidLoad method:

AudioRouter *foobar = [[AudioRouter alloc] init];
[foobar initAudioSessionRouting];
[foobar forceOutputToBuiltInSpeakers];

Now you have audio (e.g. AVAudioPlayer) output forced to loudspeaker! Note that if you plug in earphones while the app is running, then all audio output is directed to earphones.

6
votes

Swift2:

try session.setCategory(AVAudioSessionCategoryPlayAndRecord,
    withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker)
2
votes

Answer For Swift 4.0

func SetSessionPlayerOn()
{
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
    } catch _ {
    }
}
func SetSessionPlayerOff()
{
    do {
        try AVAudioSession.sharedInstance().setActive(false)
    } catch _ {
    }
}
2
votes

Swift 5

// Get the singleton instance.
let recordingSession = AVAudioSession.sharedInstance()
do {
    // Set the audio session category, mode, and options.
    try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker])
    try recordingSession.setActive(true)
} catch {
    print("Failed to set audio session category.")
}

Check the Developer Documentation for more information about AVAudioSession.CategoryOptions.

1
votes

Swift 3

 let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
    } catch let error as NSError {
        print("Audio Session error: \(error.localizedDescription)")
    }
1
votes

this is the key line to output auido in speaker instead in microphone. Note it should be set while recording

try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)

Below is complete function to setup recording

private func setupRecorder() {

    if isAudioRecordingGranted {
        let session = AVAudioSession.sharedInstance()
        do {

            if #available(iOS 10.0, *) {
                try! session.setCategory(.playAndRecord, mode:.spokenAudio)
            }
            else {
                // Workaround until https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949 isn't fixed
               session.perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
            }
            try session.setActive(true)
            try session.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)

            let settings = [
                AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 44100,
                AVNumberOfChannelsKey: 2,
                AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue
            ]
            audioRecorder = try AVAudioRecorder(url: fileUrl(), settings: settings)
            audioRecorder.delegate = self
            audioRecorder.isMeteringEnabled = true
            audioRecorder.prepareToRecord()
            self.recorderState = .Ready
        }
        catch let error {
            recorderState = .error(error)
        }
    }
    else {
        recorderState = .Failed("Don't have access to use your microphone.")
    }
}
0
votes

Swift 5: To work on both Bluetooth devices and default speaker use this:

let recordingSession = AVAudioSession.sharedInstance()
try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])