Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface AudioTapProcessor : NSObject
- @property (nonatomic, strong) AVPlayerItem *item;
- @property (nonatomic, strong) id<AudioProcessorDelegate> delegate;
- - (instancetype)initWithDelegate:(id<AudioProcessorDelegate>)delegate
- item:(AVPlayerItem *)item;
- - (void)startProcessing;
- - (void)stopProcessing;
- @end
- void init(MTAudioProcessingTapRef tap, void *clientInfo, void
- **tapStorageOut) {
- *tapStorageOut = clientInfo;
- }
- void finalize(MTAudioProcessingTapRef tap) {}
- void prepare(
- MTAudioProcessingTapRef tap,
- CMItemCount maxFrames,
- const AudioStreamBasicDescription *processingFormat
- ) {}
- void unprepare(MTAudioProcessingTapRef tap) {}
- void process(
- MTAudioProcessingTapRef tap,
- CMItemCount numberFrames,
- MTAudioProcessingTapFlags flags,
- AudioBufferList *bufferListInOut,
- CMItemCount *numberFramesOut,
- MTAudioProcessingTapFlags *flagsOut
- ) {
- //Random crashes here if I declare the delegate weak
- //Something like AUDeferredRenderer-0x7ff8f448ef (364): EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
- AudioTapProcessor *processor = (__bridge AudioTapProcessor *)MTAudioProcessingTapGetStorage(tap);
- OSStatus err = MTAudioProcessingTapGetSourceAudio(tap, numberFrames, bufferListInOut, flagsOut, NULL, numberFramesOut);
- AudioBuffer *pBuffer = &bufferListInOut->mBuffers[0];
- UInt32 frameLength = pBuffer->mDataByteSize / sizeof(float);
- float *pData = (float *)pBuffer->mData;
- if (err == noErr && processor) {
- if ([processor.delegate
- respondsToSelector:@selector(updateWith:withSize:)]) {
- [processor.delegate updateWith:pData withSize:frameLength];
- }
- }
- }
- - (void)stopProcessing
- {
- [self.item removeObserver:self forKeyPath:@"status"];
- AVMutableAudioMixInputParameters *params =
- (AVMutableAudioMixInputParameters *) _item.audioMix.inputParameters[0];
- MTAudioProcessingTapRef tap = params.audioTapProcessor;
- self.item.audioMix = nil;
- CFRelease(tap);
- //By doing this the tap processor does call its unprepare and finalize methods, so it is being deallocated fine.
- }
- var processor: AudioTapProcessor!
- override func prepareForPlayback() {
- super.prepareForPlayback()
- if processor == nil {
- processor = AudioTapProcessor(delegate: self, item: item)
- processor.startProcessing()
- }
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- player.pause()
- }
- deinit {
- //I tried to do this early in the lifecycle(viewWillDissapear) and it is the same thing.
- processor.stopProcessing()
- }
Add Comment
Please, Sign In to add comment