我们如何在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用于重置栏 .
编辑1:这是相当混乱的原因如果AVAudioPlayerNode没有一个类来维护它为什么它允许我使用率?抱歉,完全没有编程 .
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()
谢谢大家的帮助 .
1 回答
要使用AVAudioEngine更改速率,您有两种选择:
与AVAudioEnvironmentNode进行3D混合 . 只有AVAudioEnvironmentNode具有有意义的AVAudio3DMixing协议
rate
属性 .通过AVAudioUnitTimePitch节点传递文件输出并调整其
rate
.