gpt4 book ai didi

iphone - 清除 OpenAL 以加载新声音

转载 作者:行者123 更新时间:2023-11-29 13:41:09 24 4
gpt4 key购买 nike

我正在将一组初始的 100 多个声音加载到 OpenAL 缓冲区中。一段时间后,我正在清除 OpenAL 上下文并重新加载另一组声音。但是新加载的文件没有播放。看来之前的OpenAL buffer并没有真正释放出来。

这是我初始化和销毁​​ 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];

知道哪里出了问题吗?

最佳答案

经过将近一周的分析,我发现了这个错误。问题是我没有关闭 AudioFile。在我使用 fileID 之后添加这段代码后一切正常

AudioFileClose(fileID);

关于iphone - 清除 OpenAL 以加载新声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com