【问题标题】:Force audio file playback through iPhone loud speaker using Swift使用 Swift 通过 iPhone 扬声器强制播放音频文件
【发布时间】:2015-04-08 22:26:39
【问题描述】:

我有一个可以录制然后播放音频文件的应用程序。目前正在通过听筒扬声器播放音频。有人能告诉我 Swift 将如何处理编码以强制将音频从扬声器中输出吗?

下面是我用来播放音频文件的一个实例:

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


}

【问题讨论】:

  • 在stackoverflow中你应该分享一些你尝试过的代码,以获得帮助......
  • 那里,对不起!我希望这能让您更好地了解我想要完成的工作。

标签: xcode swift ios8 avaudioplayer


【解决方案1】:

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

从 Swift 1.2 开始,您使用 overrideOutputAudioPort 和 AVAudioSessionPortOverride。它可以通过执行以下操作来实现:

if !session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error:&error) {
   println("could not set output to speaker")
   if let e = error {
      println(e.localizedDescription)
   }
}

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

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代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    相关资源
    最近更新 更多