Hi smasher!
Hm, I've tried following what your link said, but it doesn't work for me... What I've done before is to completely shutdown the sound manager when the application is interrupted, and restore again when it's restored. That way music sounds well (via AudioVideoPlayback), but sound doesn't get restored... This is how I'm doing it:
- In the AppDelegate class, when the applicationWillResignActive method is called an eaglView method named "shutDownAudio" is called, and when the applicationDidBecomeActive method is called another method named "reinstallAudio" is called. Here are that methods:
Code:
// EAGLView.m methods:
- (void) shutDownAudio
{
if (sound) {
[sharedSoundManager stopPlayingMusic];
}
if (menu) {
[self netejaAudioMenu];
}else{
[self netejaAudioInterficie];
}
[sharedSoundManager shutdownSoundManager];
sharedSoundManager = nil;
}
- (void) reinstallAudio
{
if (!primerCop) {
[sharedSoundManager SingletonSoundManager];
}
}
Code:
// mySoundManager class:
+ (SingletonSoundManager *)sharedSoundManager {
// synchronized is used to lock the object and handle multiple threads accessing this method at
// the same time
@synchronized(self) {
// If the sharedSoundManager var is nil then we need to allocate it.
if(sharedSoundManager == nil) {
// Allocate and initialize an instance of this class
NSLog(@"SSM == nil");
[[self alloc] init];
}else {
NSLog(@"SSM != nil");
}
}
// Return the sharedSoundManager
return sharedSoundManager;
}
- (id)init {
NSLog(@"INIT SOUND");
if(self = [super init]) {
soundSources = [[NSMutableArray alloc] init];
soundLibrary = [[NSMutableDictionary alloc] init];
sourceLibrary = [[NSMutableDictionary alloc] init];
musicLibrary = [[NSMutableDictionary alloc] init];
// Set the default volume for music
backgroundMusicVolume = 1.0f;
// Set up the OpenAL
BOOL result = [self initOpenAL];
if(!result) return nil;
return self;
}
//[self release];
return nil;
}
- (BOOL) initOpenAL {
// Get the device we are going to use for sound. Using NULL gets the default device
device = alcOpenDevice(NULL);
// If a device has been found we then need to create a context, make it current and then
// preload the OpenAL Sources
if(device) {
//NSLog(@"INIT OPENAL SOUND");
// Use the device we have now got to create a context "air"
context = alcCreateContext(device, NULL);
// Make the context we have just created into the active context
alcMakeContextCurrent(context);
// Pre-create 32 sound sources which can be dynamically allocated to buffers (sounds)
NSUInteger sourceID;
for(int index = 0; index < kMaxSources; index++) {
//NSLog(@"new source:%d",index);
// Generate an OpenAL source
alGenSources(1, &sourceID);
// Add the generated sourceID to our array of sound sources
[soundSources addObject:[NSNumber numberWithUnsignedInt:sourceID]];
}
// Return YES as we have successfully initialized OpenAL
//NSLog(@"YES!");
return YES;
}
// Something went wrong so return NO
//NSLog(@"NO!");
return NO;
}
- (void) shutdownSoundManager {
@synchronized(self) {
if(sharedSoundManager != nil) {
[self dealloc];
}
}
}
- (void)dealloc {
// Loop through the OpenAL sources and delete them
for(NSNumber *numVal in soundSources) {
NSUInteger sourceID = [numVal unsignedIntValue];
alDeleteSources(1, &sourceID);
}
// Loop through the OpenAL buffers and delete
NSEnumerator *enumerator = [soundLibrary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
NSNumber *bufferIDVal = [soundLibrary objectForKey:key];
NSUInteger bufferID = [bufferIDVal unsignedIntValue];
alDeleteBuffers(1, &bufferID);
}
// Release the arrays and dictionaries we have been using
[soundLibrary release];
[soundSources release];
[sourceLibrary release];
[musicLibrary release];
// Disable and then destroy the context
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
// Close the device
alcCloseDevice(device);
sharedSoundManager = nil;
[super dealloc];
}
The music is restored well, but the sound never plays again after the app has become interrupted... Any clue about this?
Thanks in advance!
I've read something about audioSessions, but I don't understand where it have to be set, and no clue on how to use the listenerCallback function, or anything about this... I'm really unable to understand how to deal with that sound problem! Anyone can help me? :S