AVFoundation AVAudioPlayerNode错误,从iOS转换到mac os



我正在将Audio Foundation示例代码iOS转换为Swift 3 Mac OS因为看起来很容易理解

一切似乎建立正常,但我得到一个错误

2016-09-09 09:52:17.371 Imaginator_001[5456:217674] 09:52:17.370错误:AVAudioNode。mm:747: AUSetFormat: error -10868 2016-09-0909:52:17.371 Imaginator_001[5456:217674] error -10868

我不太确定为什么任何建议都会很好

import Cocoa
import AVFoundation
class MainWindowController: NSWindowController {
var engine: AVAudioEngine!
var playerA: AVAudioPlayerNode!
var playerB: AVAudioPlayerNode!
override func windowDidLoad() {
super.windowDidLoad()
// Do any additional setup after loading 
engine = AVAudioEngine()
playerA = AVAudioPlayerNode()
playerB = AVAudioPlayerNode()
playerA.volume = 0.5
playerB.volume = 0.5
//Use stereo audio file
let url = Bundle.main().urlForResource("vox", withExtension: "wav")
// Here you are creating an AVAudioFile from the sound file,
// preparing a buffer of the correct format and length and loading

// the file into the buffer.
let file = try? AVAudioFile(forReading: url!)

let buffer = AVAudioPCMBuffer(pcmFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length))
try! file!.read(into: buffer)
// This is a reverb with a cathedral preset. It's nice and ethereal
// You're also setting the wetDryMix which controls the mix between the effect and the
// original sound.
let reverb = AVAudioUnitReverb()
reverb.loadFactoryPreset(AVAudioUnitReverbPreset.cathedral)
reverb.wetDryMix = 50
// This is a distortion with a radio tower preset which works well for speech
// As distortion tends to be quite loud you're setting the wetDryMix to only 25
let distortion = AVAudioUnitDistortion()
distortion.loadFactoryPreset(AVAudioUnitDistortionPreset.speechRadioTower)
distortion.wetDryMix = 25
// Attach the four nodes to the audio engine
engine.attach(playerA)
engine.attach(playerB)
engine.attach(reverb)
engine.attach(distortion)
// Connect playerA to the reverb
engine.connect(playerA, to: reverb, format: buffer.format)
// Connect the reverb to the mixer
engine.connect(reverb, to: engine.mainMixerNode, format: buffer.format)
// Connect playerB to the distortion
engine.connect(playerB, to: distortion, format: buffer.format)
// Connect the distortion to the mixer
engine.connect(distortion, to: engine.mainMixerNode, format: buffer.format)
// Schedule playerA and playerB to play the buffer on a loop
playerA.scheduleBuffer(buffer, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)
playerB.scheduleBuffer(buffer, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)
// Start the audio engine
engine.prepare()
try! engine.start()

}//eo overide

@IBAction func play(_ sender: AnyObject) {
playerA.volume = 0.5
playerB.volume = 0.5
playerA.play()
playerB.play()
}
}

我使用单声道音频文件,而不是立体声,即使它在iso作为单声道工作。

最新更新