Reputation: 7959
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!
Upvotes: 35
Views: 34121
Reputation: 2286
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])
Upvotes: 2
Reputation: 113
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.
Upvotes: 3
Reputation: 2725
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.")
}
}
Upvotes: 1
Reputation: 302
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 _ {
}
}
Upvotes: 1
Reputation: 13527
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];
Upvotes: 17
Reputation: 6051
Swift 3
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
print("Audio Session error: \(error.localizedDescription)")
}
Upvotes: 1
Reputation: 147
This works great for me in Swift2
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker)
Upvotes: 13
Reputation: 21810
Swift2:
try session.setCategory(AVAudioSessionCategoryPlayAndRecord,
withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker)
Upvotes: 6
Reputation: 331
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];
Upvotes: 33
Reputation: 1360
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.
Upvotes: 6
Reputation:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
Upvotes: 18