使用Swift通过iPhone扬声器强制播放音频文件 [英] Force audio file playback through iPhone loud speaker using Swift

查看:397
本文介绍了使用Swift通过iPhone扬声器强制播放音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序记录,然后播放一个音频文件。目前,通过耳机扬声器播放音频。有人能告诉我Swift如何处理编码以强制音频输出扬声器吗?

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?

以下是我用来播放音频的一个实例file:

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)


}


推荐答案

编辑2017年7月:请参阅Husam对Swift 2.0解决方案的回答。

EDIT July 2017: Refer to Husam's answer for the Swift 2.0 solution.

从Swift 1.2开始,您使用overrideOutputAudioPort和AVAudioSessionPortOverride。它可以通过这样的方式实现:

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

我正在开发一个现在使用它的应用程序,我有一个名为setSessionPlayandRecord的函数,它看起来像: / p>

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

这篇关于使用Swift通过iPhone扬声器强制播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆