1
我正在將一組100+聲音加載到OpenAL緩衝區中。過了一段時間,我正在清除OpenAL上下文並重新加載另一組聲音。但新加載的文件沒有播放。看起來以前的OpenAL緩衝區並未真正釋放。清除OpenAL以加載新聲音
這裏是我的初始化和銷燬OpenAL的
- (void)initOpenAL
{
// Initialization
mDevice = alcOpenDevice(NULL); // select the 「preferred device」
if (mDevice) {
// use the device to make a context
mContext=alcCreateContext(mDevice,NULL);
// set my context to the currently active one
alcMakeContextCurrent(mContext);
}
}
- (void)cleanUpOpenAL
{
// delete the sources
for (NSNumber * sourceNumber in [soundDictionary allValues])
{
NSUInteger sourceID = [sourceNumber unsignedIntegerValue];
alDeleteSources(1, &sourceID);
}
[soundDictionary removeAllObjects];
// delete the buffers
for (NSNumber * bufferNumber in bufferStorageArray)
{
NSUInteger bufferID = [bufferNumber unsignedIntegerValue];
alDeleteBuffers(1, &bufferID);
}
[bufferStorageArray removeAllObjects];
// destroy the context
alcDestroyContext(mContext);
// close the device
alcCloseDevice(mDevice);
}
這是我如何加載聲音的循環內代碼:
NSString *filePath = [[NSBundle mainBundle] pathForResource:subString ofType:@"wav"];
AudioFileID fileID = [self openAudioFile:filePath];
// find out how big the actual audio data is
UInt32 fileSize = (UInt32)[self audioFileSize:fileID];
// this is where the audio data will live for the moment
unsigned char * outData = malloc(fileSize);
// this where we actually get the bytes from the file and put them
// into the data buffer
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);
if (result != 0) NSLog(@"cannot load effect: %@",fileName);
NSUInteger bufferID;
// grab a buffer ID from openAL
alGenBuffers(1, &bufferID);
if((alGetError()) != AL_NO_ERROR) {
printf("Error!");
}
// jam the audio data into the new buffer
alBufferData(bufferID,AL_FORMAT_STEREO16,outData,fileSize,44100);
if((alGetError()) != AL_NO_ERROR) {
printf("Error!");
}
// clean up the buffer
if (outData)
{
free(outData);
}
else
{
outData = NULL;
}
// save the buffer so I can release it later
[bufferStorageArray addObject:[NSNumber numberWithUnsignedInteger:bufferID]];
NSUInteger sourceID;
// grab a source ID from openAL
alGenSources(1, &sourceID);
// attach the buffer to the source
alSourcei(sourceID, AL_BUFFER, bufferID);
// store this for future use
[soundDictionary setObject:[NSNumber numberWithUnsignedInt:sourceID] forKey: [NSNumber numberWithUnsignedInt:i+(padNumber*10)]];
subString = NULL;
filePath = NULL;
這是我重裝之前我在做什麼一套新的聲音:
[self cleanUpOpenAL];
[self initOpenAL];
任何想法,事情會出錯?
尚未回覆。有沒有人有機會看看這個? – 2012-02-02 05:54:34