我们如何在 AVAudioEngine 中使用 rate?
这是我的完整代码:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioEngine = AVAudioEngine()
var myPlayer = AVAudioPlayerNode()
override func viewDidLoad() {
super.viewDidLoad()
if var filePath = NSBundle.mainBundle().pathForResource("music", ofType: "wav"){
var filePathURL = NSURL.fileURLWithPath(filePath)
audioEngine.attachNode(myPlayer)
var audioFile = AVAudioFile(forReading: filePathURL, error: nil)
var audioError:NSError?
audioEngine.connect(myPlayer, to: audioEngine.mainMixerNode, format: audioFile.processingFormat)
myPlayer.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
audioEngine.startAndReturnError(&audioError)
}else {
println("url not found")
}
}
@IBAction func playSound(sender: UIButton) {
myPlayer.rate = 0.5
myPlayer.play()
}
因此,当我播放声音时,它根本不使用速率。此外,播放完音频后如何重置位置。AVAudioPlayer 有 currentTime 用于重置栏。
Edit1:如果 AVAudioPlayerNode 没有一个类来维护,这是相当令人困惑的原因,为什么它允许我使用 rate 呢?对不起,完全不懂编程。
Edit2:在 Matt 的示例代码的帮助下工作。我错过了音频输出线:
var audioEngine = AVAudioEngine()
var myPlayer = AVAudioPlayerNode()
audioEngine.attachNode(myPlayer)
var changeAudioUnitTime = AVAudioUnitTimePitch()
changeAudioUnitTime.rate = audioChangeNumber
audioEngine.attachNode(changeAudioUnitTime)
audioEngine.connect(myPlayer, to: changeAudioUnitTime, format: nil)
audioEngine.connect(changeAudioUnitTime, to: audioEngine.outputNode, format: nil)
audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
audioEngine.startAndReturnError(nil)
audioPlayerNode.play()
感谢大家的帮助。