在 Swift 中将 AVAudioPlayer 输出更改为扬声器?

Change AVAudioPlayer output to speaker in Swift?

我的 iphone 应用程序中有简单的 AVAudioPlayer,它可以工作,但输出应该是 phone 扬声器而不是头部 phone。我在这里找到 similiar topic,但不知道如何 'translate' 到 swift(AudioSessionSetProperty 让我感到困惑)?

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

我能理解这是多么令人困惑,因为我刚刚找到答案。因此 AudioSessionSetProperty 在 iOS 7.

中被弃用

添加这个:

session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: nil)

确保先致电 AVAudioSession.sharedInstance()

在 Swift 3 或 4:

let audioSession = AVAudioSession.sharedInstance()

do {
    try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
    print("audioSession error: \(error.localizedDescription)")
}

为了避免OSStatus错误-50(user462990在评论中提到),overrideOutputAudioPort必须在设置会话类别(代码如下)后调用。

do {
    try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
    print("setCategory error: \(error.localizedDescription)")
}

试试这些 function.they 很有魅力。

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 _ {
    }
}
  func SetEarSepeakerOn()
    {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.none)
        } catch _ {
        }
    }

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

        }
    }

您可以在会话设置期间将音频会话默认设置为内置扬声器而不是接收器。

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