Search code examples
iosiphoneswiftavaudioplayer

Change AVAudioPlayer output to speaker in Swift?


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

Solution

  • 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.