cpunerd
cpunerd

Reputation: 31

iOS AudioSessionInitialize Error

I'm writing an iPad app that uses an Audio Queue. I'm running into an error resulting from calling AudioSessionInitialize more than once. I'm trying to find a way to test whether or not AudioSessionInitialize has already been called to avoid this, but so far no luck. Does anyone have a way to do this?

Thanks in advanced.

Upvotes: 0

Views: 1911

Answers (2)

hotpaw2
hotpaw2

Reputation: 70693

Since the audio session is currently a global property, you could set a C global variable flag when initializing, and then check this global variable before attempting to (re)initialize. However a dispatch_once block would be less likely to incur race condition bugs if your app is multi-threaded.

Upvotes: 1

Simon Lawrence
Simon Lawrence

Reputation: 574

You could just wrap it in a dispatch_once block as per:

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    AudioSessionInitialize(NULL, NULL, InterruptionListenerCallback, clientData);
    // Perform other setup here...
});

although you may find it easier in the long term to use implicit initialisation of your session and handle events through a delegate, as discussed here:

Audio Session Programming Guide - Initializing Your Audio Session

Upvotes: 2

Related Questions