Force audio file playback through iPhone loud speaker using Swift

主宰稳场 提交于 2019-12-06 00:37:55

问题


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)


}

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/29526435/force-audio-file-playback-through-iphone-loud-speaker-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!