OpenAL streams and breaks

I made an iphone application that uses OpenAL to play many sounds. These sounds in mp3 are quite heavy (over 1 million), and I translate them (2 buffers per sound) in order to use less memory. To control interrupts, I use this code:

In the OpenALSupport.c file:

//used to disable openAL during a call void openALInterruptionListener ( void *inClientData, UInt32 inInterruptionState) { if (inInterruptionState == kAudioSessionBeginInterruption) { alcMakeContextCurrent (NULL); } } //used to restore openAL after a call void restoreOpenAL(void* a_context) { alcMakeContextCurrent(a_context); } 

In my SoundManager.m file:

  - (void) restoreOpenAL { restoreOpenAL(mContext); } //OPENAL initialization - (bool) initOpenAL { // Initialization mDevice = alcOpenDevice(NULL); if (mDevice) { ... // use the device to make a context mContext=alcCreateContext(mDevice,NULL); // set my context to the currently active one alcMakeContextCurrent(mContext); AudioSessionInitialize (NULL, NULL, openALInterruptionListener, mContext); NSError *activationError = nil; [[AVAudioSession sharedInstance] setActive: YES error: &activationError]; NSError *setCategoryError = nil; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &setCategoryError]; ... } 

And finally, in my AppDelegate:

 - (void)applicationDidBecomeActive:(UIApplication *)application { [[CSoundManager getInstance] restoreOpenAL]; ... } 

With this method, sounds are returned after the call, but the streams seem to play randomly. Is there a specific way to control interruption with streaming sounds? I can not find an article about this.

Thank you for your help.

+4
source share
1 answer

OK, I answer my question.

I solved the problem by setting an error in the streaming method:

 - (void) updateStream { ALint processed; alGetSourcei(sourceID, AL_BUFFERS_PROCESSED, &processed); while(processed--) { oldPosition = position; NSUInteger buffer; alSourceUnqueueBuffers(sourceID, 1, &buffer); //////////////////// //code freshly added ALint err = alGetError(); if (err != 0) { NSLog(@"Error Calling alSourceUnQueueBuffers: %d",err); processed++; //restore old position for the next buffer position = oldPosition; usleep(10000); continue; } //////////////////// [self stream:buffer]; alSourceQueueBuffers(sourceID, 1, &buffer); //////////////////// //code freshly added err = alGetError(); if (err != 0) { NSLog(@"Error Calling alSourceQueueBuffers: %d",err); processed++; usleep(10000); //restore old position for the next buffer position = oldPosition; } /////////////////// } 

}

+1
source

Source: https://habr.com/ru/post/1335380/


All Articles