I have simple AVAudioPlayer in my iphone app, it works, but output should be phone speaker instead of headphone. I found similiar topic here , but don't know how to 'translate' that to swift (AudioSessionSetProperty is confusing me)?
var audioPlayer = AVAudioPlayer(data: fileData, error: &playbackError)
//fileData is url to file
if let player = audioPlayer{
player.delegate = self
if(player.prepareToPlay() && player.play()){
println("Started playing the recorded audio")
} else {
println("Could not play the audio")
}
}
try these function.they are work charming.
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 _ {
}
}
I can understand how this is confusing as I just figured out the answer. So AudioSessionSetProperty
was deprecated in iOS 7.
Add this:
session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: nil)
Make sure to call AVAudioSession.sharedInstance()
first.
In Swift 3 or 4:
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
To avoid the OSStatus
error -50 (mentioned by user462990 in the comments), overrideOutputAudioPort
has to be called after setting the session category (code below).
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
print("setCategory error: \(error.localizedDescription)")
}
func SetEarSepeakerOn()
{
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.none)
} catch _ {
}
}
swift 5
func setSessionPlayerOn() {
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
} catch _ {
}
}
You may set the audio session defaults to the built-in speaker instead of the receiver during the session setup.
do {
// 1) Configure your audio session category, options, and mode
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: [.defaultToSpeaker])
// 2) Activate your audio session to enable your custom configuration
try session.setActive(true)
} catch let error as NSError {
print("Failed to set the audio session category and mode: \(error.localizedDescription)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With