AVAudioPlayerNode 不播放声音 [英] AVAudioPlayerNode doesn't play sound
本文介绍了AVAudioPlayerNode 不播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码生成声音.一切都很好,没有错误.但是当我执行这段代码时,没有声音.我该如何解决这个问题?
I'm trying to generate sound with code below. Everthing is fine there is no error. But when I executed this code, there is no sound. How can I fix this problem ?
顺便说一下,我正在使用这个例子:http://www.tmroyal.com/playing-sounds-in-swift-audioengine.html
By the way, I'm using this example : 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()
推荐答案
您的 AVAudioEngine
看起来像是一个局部变量 - 将超出范围并被释放.将它分配给一个类实例变量,也许你会听到一些声音.
Your AVAudioEngine
looks like it's a local variable - that will go out of scope and be deallocated. Assign it to a class instance variable and maybe you'll hear some sound.
这篇关于AVAudioPlayerNode 不播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文