Hi Muthu. Thanks for the suggestion.
I don’t want to do that because failure to create the AudioQueue should mean failure to create the AudioPlayer itself. -Kenny > On Sep 25, 2017, at 6:33 PM, somu subscribe <somu.subscr...@gmail.com> wrote: > > Hi Kenny, > > You could use a lazy var and initialise it with a closure. > > class PDAudioPlayer { > > //Would be created lazily when first invoked > lazy var mQueue : AudioQueueRef = { > > //create an audioQueue and return that instance > return audioQueue > }() > } > > Thanks and regards, > Muthu > > >> On 26 Sep 2017, at 9:12 AM, Kenny Leung via swift-users >> <swift-users@swift.org <mailto:swift-users@swift.org>> wrote: >> >> Hi All. >> >> I’m trying to implement the AudioQueue example from Apple in Swift, and keep >> it as Swifty as I can. I’ve run into a problem where I have a let ivar for >> the AudioQueue, but the only way to initialize that let ivar is to pass a >> block to the function that creates the AudioQueue. I get the error, >> "Variable 'self.mQueue' used before being initialized”. Is there any way to >> get around this catch? >> >> Thanks! >> >> -Kenny >> >> The code follows. >> >> ———8<————8<———— >> class PDAudioPlayer { >> static let kNumberBuffers :Int = 3 // 1 >> //var mDataFormat:AudioStreamBasicDescription // 2 >> let mQueue :AudioQueueRef // 3 >> //var mBuffers :[AudioQueueBufferRef] // 4 >> //var mAudioFile :AudioFileID? // 5 >> var bufferByteSize :UInt32? // 6 >> var mCurrentPacket :Int64? // 7 >> var mNumPacketsToRead :UInt32? // 8 >> var mPacketDescs :UnsafePointer<AudioStreamPacketDescription>? // 9 >> var mIsRunning: Bool = false // 10 >> >> let dispatchQueue :DispatchQueue >> let source :PDFileAudioSource >> var audioBuffers :[PDAudioBuffer] >> >> init?(source:PDFileAudioSource) { >> self.source = source >> self.dispatchQueue = DispatchQueue(label:"AudioPlayer", >> qos:.default, attributes:[], autoreleaseFrequency:.workItem, target:nil) >> self.audioBuffers = [PDAudioBuffer]() >> >> var tempAudioQueue :AudioQueueRef? >> var dataFormat = source.mDataFormat >> let status = AudioQueueNewOutputWithDispatchQueue(&tempAudioQueue, >> &dataFormat, 0, self.dispatchQueue) { [weak self] (queue, buffer) in // >> ERROR on this line >> guard let this = self else { >> return // block >> } >> guard let audioBuffer = >> this.audioBufferForAudioQueueBuffer(aqBuffer:buffer) else { >> return // block >> } >> this.source.fillBuffer(audioBuffer) >> } >> if status != 0 { >> return nil >> } >> guard let audioQueue = tempAudioQueue else { >> return nil >> } >> self.mQueue = audioQueue >> } >> >> private func >> audioBufferForAudioQueueBuffer(aqBuffer:AudioQueueBufferRef) -> >> PDAudioBuffer? { >> for buffer in self.audioBuffers { >> if buffer.audioQueueBuffer == aqBuffer { >> return buffer >> } >> } >> return nil >> } >> } >> >> _______________________________________________ >> swift-users mailing list >> swift-users@swift.org <mailto:swift-users@swift.org> >> https://lists.swift.org/mailman/listinfo/swift-users >
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users