AudioQueueDispose延迟

u.g*_*gen 26 audio xcode objective-c audioqueueservices ios

根据此处的文档https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/#//apple_ref/c/func/AudioQueueDispose

err = AudioQueueDispose(queue, true);
Run Code Online (Sandbox Code Playgroud)

我使用true这样处理AudioQueue立即发生,虽然它有时会立即处理队列,有时它在设备上延迟3-4秒到13秒.err = AudioQueueStop(queue, true)也有同样的问题.

我的理解是两个函数都尝试刷新释放缓冲区并且即将被排队......
所以我甚至会帮助我的回调函数来刷新缓冲区,如果AudioQueueDispose要调用的话.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我要AudioQueues在播放曲目后使用录制内容,因此我需要在没有延迟的情况下返回此函数.几百毫秒还可以,但3-4秒?这是不可接受的.

其他AudioQueue函数也在同一个线程上调用,它们似乎工作正常.

我也尝试在主线程上调用它来确保它是否会改变任何东西

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

要么

dispatch_sync(dispatch_get_main_queue(),^{ 没有任何区别

知道可能会发生什么吗?

And*_*ius 1

我成功地立即停止了音频播放:

-(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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我的:

void audioCallback(void *custom_data, AudioQueueRef queue, AudioQueueBufferRef buffer)
Run Code Online (Sandbox Code Playgroud)

我只会在以下情况下将更多内容放入队列中:

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

在我的特定情况下,我播放 AAC-LC 音频。