我试着用下面的代码生成声音。一切都是好的,没有错误。但是当我执行这个代码时,没有声音。我怎样才能解决这个问题?
顺便提一下,我用的是这个例子:http://www.tmroyal.com/playing-sounds-in-swift-audioengine.html
var ae:AVAudioEngine
var player:AVAudioPlayerNode?
var mixer:AVAudioMixerNode
var buffer:AVAudioPCMBuffer
ae = AVAudioEngine()
player = AVAudioPlayerNode()
mixer = ae.mainMixerNode;
buffer = AVAudioPCMBuffer(pcmFormat: player!.outputFormat(forBus:0), frameCapacity: 100)
buffer.frameLength = 100
// generate sine wave.
var sr:Float = Float(mixer.outputFormat(forBus:0).sampleRate)
var n_channels = mixer.outputFormat(forBus:0).channelCount
var i:Int=0
while i < Int(buffer.frameLength) {
var val = sinf(441.0*Float(i)*2*Float(M_PI) / Float(sr))
buffer.floatChannelData?.pointee[i] = val * 0.5
i+=Int(n_channels)
}
// setup audio engine
ae.attach(player!)
ae.connect(player!, to: mixer, format: player!.outputFormat(forBus: 0))
ae.prepare()
try! ae.start()
// play player and buffer
player!.scheduleBuffer(buffer, at: nil, options: .loops, completionHandler: nil)
player!.play()发布于 2016-10-10 13:43:29
您的AVAudioEngine看起来像是一个局部变量--它将超出作用域并被取消分配。将其分配给类实例变量,您可能会听到一些声音。
https://stackoverflow.com/questions/39958750
复制相似问题