我正在AudioKit的帮助下开发一个应用程序。我正在尝试平移音频,这样它一次只能在一只耳朵里播放。有什么可能出错的想法吗?
声音在播放,但在两耳同时播放。
func createTone(freq: Double, dB: Int, isLeftEar: Bool){
currentFreq = Int(freq)
currentdB = dB
isAudioOn = true
osciliator.frequency = freq
osciliator.amplitude = calculateVolume(dB: dB)
osciliator.rampDuration = 0.25
let leftPan = AKPanner(osciliator, pan: -1)
let rightPan = AKPanner(osciliator, pan: 1)
if(isLeftEar){
AudioKit.AKManager.output = leftPan
}else{
AudioKit.AKManager.output = rightPan
}
do{
try AudioKit.AKManager.start()
}catch{
print("could not start AudioKit.")
}
if isLeftEar {
leftPan.start()
}else{
rightPan.start()
}
osciliator.start()
sleep(2)
do{
try AudioKit.AKManager.stop()}
catch{
print("AudioKit could not stop")
}
通常,节点是在类级别声明的,而不是作为局部变量声明的。所以您会得到如下内容:
class ViewController: UIViewController {
var panner: AKPanner!
func createTone(freq: Double, dB: Int, isLeftEar: Bool){
// ...
panner = AKPanner(oscillator, pan: (isLeftEar) ? -1 : 1)
// ...
}
此外,不应在主线程中调用sleep
。您应该使用DispatchQueue.Main.AsyncAfter
:
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
do{
try AKManager.stop()}
catch{
print("AudioKit could not stop")
}
}