在iPhone上通过OpenAL播放不同音量的不同声音时,会发出奇怪的砰砰声

c0d*_*0de 11 iphone openal objective-c ios

我正在iPhone上使用OpenAL声音框架,我在个别声音上设置了不同的音量.我遇到了一个问题,当我从一个声音切换到另一个声音时,我听到一声初始的砰砰声/咔哒声.

当我有一个高音量(1.0)的声音和一个低音量(0.2)的第二个声音时,它真的很明显.当我敲响响亮的声音,然后敲击柔和的声音时,我听到弹出/咔嗒声.但是,当我从柔和的声音走向响亮的声音时,我什么都没注意到.因此,当从响亮的声音切换到柔和的声音时,真正发生了弹出/点击

这是init声音方法:

 - (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;
    }
Run Code Online (Sandbox Code Playgroud)

这是播放功能:

- (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);
    }
Run Code Online (Sandbox Code Playgroud)

这是我在播放后立即为每个声音设置音量的功能(我也尝试过设置之前):

- (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);
 }
   } 
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助,因为我已经尝试了我能想到的一切.我会非常感激的.

Raj*_*yan 3

我所要做的就是使用 calloc 而不是 malloc 为 OpenAL 缓冲区分配内存。或者您也可以使用 memset 将内存设置为零。

奇怪的爆裂声消失了。就我而言,这是由于垃圾内存造成的。这就是为什么它也是随机的。希望这可以帮助。

  • 从 1.0f 音量的声音变为 0.05f 音量的声音时,仍然会发出爆裂声。有趣的是,它在 iPhone 4.0、iPhone 4.3 和 iPad 4.3 上运行良好。在 iPad 3.2、iPhone 4.1、iPad 4.2、iPhone 4.2 上发出爆裂声。还有其他想法吗? (2认同)