0

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.

cpunerd
  • 31
  • 3

2 Answers2

2

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

Simon Lawrence
  • 564
  • 2
  • 8
1

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.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153