代码之家  ›  专栏  ›  技术社区  ›  iwasrobbed

AVAudioPlayer在每次新分配后不卸载缓存内存

  •  1
  • iwasrobbed  · 技术社区  · 16 年前

    我在乐器中看到,当我通过苹果提供的标准“AddMusic”示例方法播放声音时,它会通过 prepareToPlay 调用(引用 AudioToolBox 框架的 Cache_DataSource::ReadBytes 功能)每次分配新播放器时(即每次播放不同的声音)。但是,缓存的数据永远不会被释放。

    你们有没有遇到过这个问题,或者我在代码中做错了什么?我有这个问题已经有一段时间了,它真的困扰着我,因为我的代码是什么苹果是逐字逐句(我想)。

    如何调用函数:

    - (void)playOnce:(NSString *)aSound {
    
    // Gets the file system path to the sound to play.
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];  
    
    // Converts the sound's file path to an NSURL object
    NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    self.soundFileURL = soundURL;
    [soundURL release];
    
    AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil];  
    self.theAudio = newAudio; // automatically retain audio and dealloc old file if new m4a file is loaded
    
    [newAudio release]; // release the audio safely
    
    // this is where the prior cached data never gets released
    [theAudio prepareToPlay];
    
    // set it up and play
    [theAudio setNumberOfLoops:0];
    [theAudio setVolume: volumeLevel];
    [theAudio setDelegate: self];
    [theAudio play];
    
    }
    

    theAudio 在监狱里被释放 dealloc

    1 回复  |  直到 16 年前
        1
  •  2
  •   iwasrobbed    16 年前

    来自另一个名为smasher的来源的答案:

    OpenAL也有类似的问题,你为每个文件加载一个声音;再加上我见过的最常见的实现一次加载整个文件,这对内存来说很难。