Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why call to CFRunLoopRunInMode() in Audio Queue Playback code?

I'm following the iOS "Audio Queue Programming Guide - Playing Audio". Near the end of the guide, there are calls to CFRunLoopRunInMode() in the step Start and Run an Audio Queue:

do {                                               // 5
    CFRunLoopRunInMode (                           // 6
        kCFRunLoopDefaultMode,                     // 7
        0.25,                                      // 8
        false                                      // 9
    );
} while (aqData.mIsRunning);
//...

The documentation about line 6 says: "The CFRunLoopRunInMode function runs the run loop that contains the audio queue’s thread." But isn't that run loop executed anyways when my method returns? The code above is executed by the main thread upon pressing the play button in my app.

Now I'm having a hard time understanding what these calls to CFRunLoopRunInMode() are good for, because they have the disadvantage that my play-button does not update correctly (it looks pressed down for the whole time that the audio plays) and there is no positive effect, i.e. the audio also plays nicely if I remove the do-while-loop from my code along with the calls to CFRunLoopRunInMode() and instead directly return from this method. Well this points to the obvious solution to simply keep these calls removed as this doesn't create a problem. Can someone explain why then this code is included in the official guide by Apple on using Audio Queues in iOS for Audio Playback?

Edit:

I'm just seeing that in Mac OS X, there exists the same audio queues API as on iOS, and the guide for iOS seems to be a copy-paste duplication of the Mac OS guide. This leads me to the suspicion that those calls to the run loop are only required in Mac OS and not anymore in iOS, e.g. because otherwise the Mac OS application would exit or something like that. Can someone please verify this or rule it out?

like image 811
Daniel S. Avatar asked Jan 08 '13 16:01

Daniel S.


1 Answers

@bunnyhero is right, CFRunLoopRunInMode() is usually for command line examples

https://github.com/abbood/Learning-Core-Audio-Book-Code-Sample/blob/master/CH05_Player/CH05_Player/main.c

As long as your AudioQueueRef is not deallocated, you dont have to use CFRunLoopRunInMode() in IOS...

What I do is create a separate class for audio queue and as long as my class pointer and AudioQueueRef is allocated I can playback, pause, resume, stop etc....

like image 138
u.gen Avatar answered Nov 07 '22 07:11

u.gen