7

我有一个基于小级别的 iPhone 应用程序。我需要为每个级别加载和发布声音文件。除了释放声音外,我的 openAL SoundManager 一切正常。

起初,当我删除声音时,它似乎做了它应该做的事情——它删除了声音,除非我重新加载它,否则我无法再次访问它。但是,当我使用“仪器”测试我的应用程序 dealloc 时,它没有显示任何解除分配。它似乎没有释放内存。因此,当您从一个级别移动到另一个级别时,内存用完并且应用程序崩溃并不需要很长时间。

我在控制台中收到此错误:

节目接收信号:“0”。警告:check_safe_call:无法恢复当前帧终止退出

这就是我加载声音的方式 -

- (void)loadSoundWithKey:(NSString*)aSoundKey fileName:(NSString*)aFileName fileExt:(NSString*)aFileExt {
// Check to make sure that a sound with the same key does not already exist
NSNumber *numVal = [soundLibrary objectForKey:aSoundKey];
// If the key is found log it and finish
if(numVal != nil) {
    NSLog(@"WARNING - SoundManager: Sound key '%@' already exists.", aSoundKey);
    return;
}
    NSUInteger bufferID;
// Generate a buffer within OpenAL for this sound
alGenBuffers(1, &bufferID);
// Set up the variables which are going to be used to hold the format
// size and frequency of the sound file we are loading
ALenum  error = AL_NO_ERROR;
ALenum  format;
ALsizei size;
ALsizei freq;
ALvoid *data;
NSBundle *bundle = [NSBundle mainBundle];
// Get the audio data from the file which has been passed in
CFURLRef fileURL = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:aFileName ofType:aFileExt]] retain];
if (fileURL)
{   
    data = MyGetOpenALAudioData(fileURL, &size, &format, &freq);
    CFRelease(fileURL);
    if((error = alGetError()) != AL_NO_ERROR) {
        NSLog(@"ERROR - SoundManager: Error loading sound: %x\n", error);
        exit(1);
    }
    // Use the static buffer data API
    alBufferDataStaticProc(bufferID, format, data, size, freq);
    if((error = alGetError()) != AL_NO_ERROR) {
        NSLog(@"ERROR - SoundManager: Error attaching audio to buffer: %x\n", error);
    }       
}
else
{
    NSLog(@"ERROR - SoundManager: Could not find file '%@.%@'", aFileName, aFileExt);
    data = NULL;
}

// Place the buffer ID into the sound library against |aSoundKey|
[soundLibrary setObject:[NSNumber numberWithUnsignedInt:bufferID] forKey:aSoundKey];
if(DEBUG) NSLog(@"INFO - SoundManager: Loaded sound with key '%@' into buffer '%d'", aSoundKey, bufferID);

}

这就是我试图删除/释放的方式。但它似乎仍然保留了声音文件的记忆——

- (void)removeSoundWithKey:(NSString*)aSoundKey {
// Find the buffer which has been linked to the sound key provided
NSNumber *numVal = [soundLibrary objectForKey:aSoundKey];
// If the key is not found log it and finish
if(numVal == nil) {
    NSLog(@"WARNING - SoundManager: No sound with key '%@' was found so cannot be removed", aSoundKey);
    return;
}    
// Get the buffer number form the sound library so that the sound buffer can be released
NSUInteger bufferID = [numVal unsignedIntValue];
alDeleteBuffers(1, &bufferID);
[soundLibrary removeObjectForKey:aSoundKey];
if(DEBUG) NSLog(@"INFO - SoundManager: Removed sound with key '%@'", aSoundKey);

}

谁能想到完全删除我的声音文件的每一个痕迹(能够再次加载它)?

非常感谢你!

4

3 回答 3

5

如果有人感兴趣...我的问题通过将 alBufferDataStaticProc 更改为 alBufferData 得到解决。然后添加if(data) free(data);见下文。

谢谢你的帮助。

alBufferData(bufferID, format, data, size, freq);
    if((error = alGetError()) != AL_NO_ERROR) {
        NSLog(@"ERROR - SoundManager: Error attaching audio to buffer: %x\n", error);
    }
    if (data) 
        free(data);
}else{
    NSLog(@"ERROR - SoundManager: Could not find file '%@.%@'", fileName, fileType);
    data = NULL;
于 2010-01-30T23:00:53.967 回答
2

这是我使用相同的苹果示例和一些openAL 文档“完全删除我的声音文件的每个痕迹(能够再次加载它) ”所做的总结:

if (device != NULL)
    [self teardownOpenAL];
[self initOpenAL];
alSourcePlay(source);

这里的主要补充是if,在我的情况下,我实际上添加到teardownOpenAL.

于 2011-01-10T14:18:20.150 回答
0

不熟悉 iPhone 版 OpenAL 的特色,但这是我的粗略猜测:

缓冲区是否仍附加到 OpenAL 源,即您是否在加载和释放之间调用了 alSourcei(..., AL_BUFFER, ...) 或 alSourceQueueBuffers?如果是这种情况,您可能需要在删除缓冲区之前运行 alSourcei(..., AL_BUFFER, NULL) 或 alSourceUnqueueBuffers。

在调用 alDeleteBuffers 后尝试检查 OpenAL 错误 (alGetErrors)。

于 2010-01-18T16:40:13.180 回答