Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force audio file playback through iPhone loud speaker using Swift

I have an App that records and then plays back an audio file. Currently the audio playback is playing through the earpiece speaker. Could someone tell me how Swift would handle coding this to force the audio out the loud speaker instead?

Below is a one of the instances I'm using to play the audio file:

@IBAction func playAudioVader(sender: UIButton) {
    playAudioWithVariablePitch(-1000)
    }

    func playAudioWithVariablePitch(pitch: Float){
    audioPlayer.stop()
    audioEngine.stop()
    audioEngine.reset()

        var audioPlayerNode = AVAudioPlayerNode()
        audioEngine.attachNode(audioPlayerNode)

        var changePitchEffect = AVAudioUnitTimePitch()
        changePitchEffect.pitch = pitch
        audioEngine.attachNode(changePitchEffect)

        audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)
        audioEngine.connect(changePitchEffect, to: audioEngine.outputNode, format: nil)

        audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
        audioEngine.startAndReturnError(nil)

        audioPlayerNode.play()

}

   override func viewDidLoad() {

    super.viewDidLoad()

    audioPlayer = AVAudioPlayer(contentsOfURL:receivedAudio.filePathURL, error: nil)
    audioPlayer.enableRate = true
    audioEngine = AVAudioEngine()
    audioFile = AVAudioFile(forReading:receivedAudio.filePathURL, error: nil)


}
like image 993
Unconquered82 Avatar asked Apr 08 '15 22:04

Unconquered82


2 Answers

EDIT July 2017: Refer to Husam's answer for the Swift 2.0 solution.

As of Swift 1.2, you use overrideOutputAudioPort and AVAudioSessionPortOverride. It can be implemented by doing something like this:

if !session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error:&error) {
   println("could not set output to speaker")
   if let e = error {
      println(e.localizedDescription)
   }
}

I'm working on an app that uses this now, and I have a function called setSessionPlayandRecord, which looks like:

func setSessionPlayAndRecord() {
    let session:AVAudioSession = AVAudioSession.sharedInstance()
    var error: NSError?
    if !session.setCategory(AVAudioSessionCategoryPlayAndRecord, error:&error) {
        println("could not set session category")
        if let e = error {
            println(e.localizedDescription)
        }
    }
    if !session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error:&error) {
        println("could not set output to speaker")
        if let e = error {
            println(e.localizedDescription)
        }
    }
    if !session.setActive(true, error: &error) {
        println("could not make session active")
        if let e = error {
            println(e.localizedDescription)
        }
    }
}
like image 93
djflorio Avatar answered Apr 29 '23 13:04

djflorio


Swift 2.0 Code

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 _ {
    }
}
like image 36
Husam Avatar answered Apr 29 '23 11:04

Husam