Change AVAudioPlayer output to speaker in Swift?

18,903

Solution 1

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.

Solution 2

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)")
}

Solution 3

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

Solution 4

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

        }
    }

Solution 5

  func SetEarSepeakerOn()
    {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.none)
        } catch _ {
        }
    }
Share:
18,903
Marko
Author by

Marko

Updated on July 21, 2022

Comments

  • Marko
    Marko almost 2 years

    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")
        }
    }