Change AVAudioPlayer output to speaker in Swift?
Asked Answered
L

7

10

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")
    }
}
Lyudmila answered 20/2, 2015 at 14:3 Comment(0)
K
23

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.

Kutch answered 11/4, 2015 at 14:51 Comment(1)
for Swift 3-4 use the code below: try recordingSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)Kurrajong
L
17

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)")
}
Lexicography answered 21/2, 2017 at 2:43 Comment(3)
Could you please show this code integrated into Marko's code? I see an AVAudioPlayer in his code and an AVAudioSession in yours. How do these tie together? I tried adding your code into my project but keep getting errors.Pathogenic
I am using exactly this code but I'm getting an error on execution "The operation couldn’t be completed. (OSStatus error -50.)" any ideas?Habsburg
Here is a bit more of an explanation of what I found and what solved things for me when trying to implement a Navigation style prompt to car stereo or phone speaker if nothing connected to the phone. Hope this helps someone else ossh.com.au/design-and-technology/software-development/…Contumelious
M
8

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 _ {
    }
}
Mindamindanao answered 1/3, 2018 at 19:4 Comment(5)
You Just Need To Call The SetSessionPlayerOn() Whenever You need.Mindamindanao
Is there a way to change from speaker to built-in earpiece. I'm working on audio call but by default it's on speaker and there's no way I can manage to change.Enfeeble
Have You tried the above code with replacement of boolean value with their negative value? @NijMindamindanao
Yes and that makes the speaker off. What i want is to activate earpiece which is near to front camera. NOTE: I am using opentok library for making video/audio call.Enfeeble
I also want to implement built-in earpiece on Opentok library for making audio call. Can anyone help me on this.Perl
R
2

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)")
        }
Richly answered 3/2, 2021 at 4:32 Comment(0)
D
1
  func SetEarSepeakerOn()
    {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.none)
        } catch _ {
        }
    }
Disendow answered 4/7, 2018 at 10:6 Comment(0)
D
1

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 _ {

        }
    }
Drug answered 13/2, 2020 at 4:8 Comment(0)
S
0

Swift 5

All of these answers are either outdated or not permanent solutions.

As the docs describe, If you’d prefer to permanently enable this behavior, you should instead set the category’s defaultToSpeaker option like so:

try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker])

Setting this option routes to the speaker rather than the receiver if no other accessory such as headphones are in use.

Soubriquet answered 13/6, 2022 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.