AVAudioPlayerNode не воспроизводит звук

Я пытаюсь создать звук с помощью кода ниже. Все нормально, ошибок нет. Но когда я выполнил этот код, звука нет. Как я могу исправить эту проблему?

Кстати, я использую этот пример: 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()

person BadCode    schedule 10.10.2016    source источник


Ответы (1)


Ваш AVAudioEngine выглядит как локальная переменная - она ​​выйдет из области видимости и будет освобождена. Назначьте его переменной экземпляра класса, и, возможно, вы услышите какой-нибудь звук.

person Rhythmic Fistman    schedule 10.10.2016