Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
    }
}
like image 792
Marko Avatar asked Feb 20 '15 14:02

Marko


6 Answers

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 _ {
    }
}
like image 137
Ali Pishvaee Avatar answered Sep 28 '22 13:09

Ali Pishvaee


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.

like image 21
ohhmaar Avatar answered Sep 28 '22 15:09

ohhmaar


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)")
}
like image 35
kauai Avatar answered Sep 28 '22 15:09

kauai


  func SetEarSepeakerOn()
    {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.none)
        } catch _ {
        }
    }
like image 23
Sachin Kishore Avatar answered Sep 28 '22 13:09

Sachin Kishore


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

        }
    }
like image 42
Ahmed Safadi Avatar answered Sep 28 '22 13:09

Ahmed Safadi


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)")
        }
like image 37
Duke Caesar Avatar answered Sep 28 '22 15:09

Duke Caesar