Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AudioQueueDispose delay

According to documentation here https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/#//apple_ref/c/func/AudioQueueDispose

err = AudioQueueDispose(queue, true);

I use true so dispose of AudioQueue happens immediately, although it does dispose queue immediately sometimes , other times it has delay 3-4 seconds up to 13 seconds on the device. err = AudioQueueStop(queue, true) has the same problem as well.

My understanding is that both functions try to flush-release buffers already and about to be enqueued...
so I even help my call-back function to flush the buffers if AudioQueueDispose is going to be called.

static void MyAQOutputCallBack(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inCompleteAQBuffer)
{
  if (player.shouldDispose) {
        printf("player shouldDispose !!!!!!!!!!!\n\n\n\n\n\n");
        OSStatus dispose = AudioQueueFlush (inAQ);
        return;
    }
}

Since I am going to record something using AudioQueues after playing a track, I need this functions returned without delays. couple hundred milliseconds is okay but 3-4 seconds? that is unacceptable.

Other AudioQueue functions also being called on the same thread and they seem working fine.

I have also tried to call this on main thread to make sure if it is going to change anything or not

[self performSelectorOnMainThread:@selector(tryOnMain) withObject:nil waitUntilDone:NO];

or

dispatch_sync(dispatch_get_main_queue(),^{ didnt do any difference

Any idea what might be happening?

like image 821
u.gen Avatar asked Jul 17 '15 13:07

u.gen


1 Answers

I successfully immediately stop my audio playback by:

-(void)stopAudio
    {
        @synchronized(audioLock) {
            audioLock=[NSNumber numberWithBool:false];
            OSStatus err;
            err=AudioQueueReset (_audioQueue);
            if (err != noErr)
            {
                NSLog(@"AudioQueueReset() error: %d", (int)err);
            }
            err=AudioQueueStop (_audioQueue, YES);
            if (err != noErr)
            {
                NSLog(@"AudioQueueStop() error: %d", (int)err);
            }
            err=AudioQueueDispose (_audioQueue, YES);
            if (err != noErr)
            {
                NSLog(@"AudioQueueDispose() error: %d", (int)err);
            }
        }
    }

And in my:

void audioCallback(void *custom_data, AudioQueueRef queue, AudioQueueBufferRef buffer)

I only put more stuff in my queue if:

myObject *weakSelf = (__bridge myObject *)custom_data;
@synchronized(weakSelf -> audioLock) {
    if ([weakSelf -> audioLock boolValue]) {
         Put_more_stuff_on_queue
    }

In my particular case I playback AAC-LC audio.

like image 150
Anders Cedronius Avatar answered Nov 11 '22 17:11

Anders Cedronius