Hi there!
I'm adding sound to my app using OpenAL and I've found some really strange troubles whit it... First of all, seems that it doesn't work in 3.1.2 OS, and seems to work better on 3.0 OS. I'll show my OpenAL's class code and I'll explain what is happening each time:
// SingletonSoundManager class:
Code:
- (void) loadSoundWithKey:(NSString*)theSoundKey
fileName:(NSString*)theFileName
fileExt:(NSString*)theFileExt
frequency:(NSUInteger)theFrequency {
// Get the full path of the audio file
NSString *filePath = [[NSBundle mainBundle] pathForResource:theFileName ofType:theFileExt];
// Now we need to open the file
AudioFileID fileID = [self openAudioFile:filePath];
// Find out how big the actual audio data is
UInt32 fileSize = [self audioFileSize:fileID];
// Create a location for the audio data to live temporarily
unsigned char *outData = malloc(fileSize);
// Load the byte data from the file into the data buffer
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, FALSE, 0, &fileSize, outData);
AudioFileClose(fileID);
if(result != 0) {
NSLog(@"ERROR SoundEngine: Cannot load sound: %@", theFileName);
return;
}
NSUInteger bufferID;
// Generate a buffer within OpenAL for this sound
alGenBuffers(1, &bufferID);
// Place the audio data into the new buffer
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, theFrequency);
// Save the buffer to be used later
[soundLibrary setObject:[NSNumber numberWithUnsignedInt:bufferID] forKey:theSoundKey];
// Clean the buffer
if(outData) {
free(outData);
outData = NULL;
}
}
- (NSUInteger) playSoundWithKey:(NSString*)theSoundKey gain:(ALfloat)theGain pitch:(ALfloat)thePitch location:(Vector2f)theLocation shouldLoop:(BOOL)theShouldLoop {
ALenum err = alGetError(); // clear the error code
// Find the buffer linked to the key which has been passed in
NSNumber *numVal = [soundLibrary objectForKey:theSoundKey];
if(numVal == nil) return 0;
NSUInteger bufferID = [numVal unsignedIntValue];
// Find an available source i.e. it is currently not playing anything
NSUInteger sourceID = [self nextAvailableSource];
// Make sure that the source is clean by resetting the buffer assigned to the source
// to 0
alSourcei(sourceID, AL_BUFFER, 0);
//Attach the buffer we have looked up to the source we have just found
alSourcei(sourceID, AL_BUFFER, bufferID);
// Set the pitch and gain of the source
alSourcef(sourceID, AL_PITCH, thePitch);
alSourcef(sourceID, AL_GAIN, theGain);
// Set the looping value
if(theShouldLoop) {
alSourcei(sourceID, AL_LOOPING, AL_TRUE);
} else {
alSourcei(sourceID, AL_LOOPING, AL_FALSE);
}
// Check to see if there were any errors
err = alGetError();
if(err != 0) {
NSLog(@"ERROR SoundManager: %d", err);
return 0;
}
NSNumber *num = [NSNumber numberWithUnsignedInt:sourceID];
[sourceLibrary setObject:num forKey:theSoundKey];
//NSLog(@"PLAY1: Source %@:%u",theSoundKey,sourceID);
//NSLog(@"PLAY2: Source %@:%u",theSoundKey,[[sourceLibrary objectForKey:theSoundKey] unsignedIntValue]);
// Now play the sound
alSourcePlay(sourceID);
// Return the source ID so that loops can be stopped etc
return sourceID;
}
// My EAGLView's method to load an play sounds:
Code:
sharedSoundManager = [SingletonSoundManager sharedSoundManager];
[sharedSoundManager loadSoundWithKey:@"morGrossa" fileName:@"XofGrossa" fileExt:@"caf" frequency:44100];
[sharedSoundManager loadSoundWithKey:@"morBorinot" fileName:@"XofGrossa" fileExt:@"caf" frequency:44100];
[sharedSoundManager loadSoundWithKey:@"flit" fileName:@"Spray" fileExt:@"caf" frequency:44100];
(...)
[sharedSoundManager playSoundWithKey:@"morBorinot" gain:1.0f pitch:1.0f location:Vector2fZero shouldLoop:NO];
(...)
Now, let's see what happens (always talking about devices, not simulator):
- 3.0 OS: Sounds are loaded well but when some sounds are played game suddenly freezes for about 2 or 3 seconds, then screen turns black and the Apple's logo is shown. After 10 or more seconds the device shows the locked screen like if nothing have happened and no crash log is generated. Really really strange, because it only occurs with some sounds...
- 3.1.2 OS: Even stranger maybe. Sound loading isn't done as it should do and XCode's console shows the following:
Code:
...[2589:207] Funcio carregaAudioInterficie
...[2589:207] ERROR SoundEngine: Cannot open file: /var/mobile/Applications/2CDC0469-1031-4339-B226-6F90DBC36836/myGame.app/Xof2.caf
...[2589:207] ERROR: cannot file file size
...[2589:207] ERROR SoundEngine: Cannot load sound: Xof2
Of course files are stored in the same directory as the project, are imported to the project, and they are loaded well in 3.0 OS... When game is running some sounds doesn't sound right (only noise), but some others sound well.
Have anyone any clue about this? Maybe I should call Dr. Holmes, or an exorcist? Any help will be really appreciated because I have no idea of what can be happening...
Thanks in advance!