我在 iPhone 上使用 OpenAL 声音框架,并为各个声音设置不同的音量。我遇到了一个问题,从一种声音切换到另一种声音时,我听到了初始的爆裂声/咔嗒声。
当我的一个声音具有高音量 (1.0) 和另一个声音具有低音量 (0.2) 时,这真的很明显。当我击中响亮的声音,然后击中柔和的声音时,我会听到爆裂声/咔嗒声。但是当我从柔和的声音变为响亮的声音时,我什么都没有注意到。所以当从响亮的声音切换到柔和的声音时,确实会发生弹出/咔嗒声。
这是初始化声音方法:
- (id) initWithSoundFile:(NSString *)file doesLoop:(BOOL)loops
{
self = [super init];
if (self != nil)
{
if(![self loadSoundFile:file doesLoop:loops])
{
debug(@"Failed to load the sound file: %@...", file);
[self release];
return nil;
}
self.sourceFileName = file;
//temporary sound queue
self.temporarySounds = [NSMutableArray array];
//default volume/pitch
self.volume = 1.0;
self.pitch = 1.0;
}
return self;
}
这是播放功能:
- (BOOL) play
{
if([self isPlaying]) //see if the base source is busy...
{
//if so, create a new source
NSUInteger tmpSourceID;
alGenSources(1, &tmpSourceID);
//attach the buffer to the source
alSourcei(tmpSourceID, AL_BUFFER, bufferID);
alSourcePlay(tmpSourceID);
//add the sound id to the play queue so we can dispose of it later
[temporarySounds addObject: [NSNumber numberWithUnsignedInteger:tmpSourceID]];
//a "callback" for when the sound is done playing +0.1 secs
[self performSelector:@selector(deleteTemporarySource)
withObject:nil
afterDelay:(duration * pitch) + 0.1];
return ((error = alGetError()) != AL_NO_ERROR);
}
//if the base source isn't busy, just use that one...
alSourcePlay(sourceID);
return ((error = alGetError()) != AL_NO_ERROR);
}
这是我在播放后立即为每个声音设置音量的功能(我也尝试在播放前设置它):
- (void) setVolume:(ALfloat)newVolume
{
volume = MAX(MIN(newVolume, 1.0f), 0.0f); //cap to 0-1
alSourcef(sourceID, AL_GAIN, volume);
//now set the volume for any temporary sounds...
for(NSNumber *tmpSourceID in temporarySounds)
{
//tmpSourceID is the source ID for the temporary sound
alSourcef([tmpSourceID unsignedIntegerValue], AL_GAIN, volume);
}
}
非常感谢任何帮助,因为我已经尝试了我能想到的一切。我会很感激的。