1

我一直在使用一个类来使用 AVAudioPlayer 播放声音。因为我想在播放完这些声音后立即释放它们,所以我添加了一个委托。这会在声音完成播放后,但在我的 -audioPlayerDidFinishPlaying 被调用之前导致“_NSAutoreleaseNoPool():自动释放类 NSCFString 的对象 0x55e060 且没有池到位 - 只是泄漏”错误。

以下是一些来源:

@interface MyAVAudioPlayer : NSObject <AVAudioPlayerDelegate> {
    AVAudioPlayer   *player;
    float           savedVolume;
    BOOL            releaseWhenDone;
}

主类.m:

- (MyAVAudioPlayer *) initPlayerWithName: (NSString *) name;
{
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: name ofType: @"caf"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    [fileURL release];
    [player prepareToPlay];
    return (self);
}
- (MyAVAudioPlayer *)getAndPlayAndRelease:(NSString *)name withVolume:(float) vol;
{
    MyAVAudioPlayer *newMyAVPlayer = [self initPlayerWithName:name];
    player.volume = vol;
    [player play];
    releaseWhenDone = YES;
    [player setDelegate: self];
    return newMyAVPlayer;
}   
+ (void) getAndPlayAndReleaseAuto:(NSString *)name withVolume:(float) vol;
{
    MyAVAudioPlayer *newMyAVPlayer = [[MyAVAudioPlayer alloc] getAndPlayAndRelease:name withVolume:vol];
//  [newMyAVPlayer autorelease];
}

#pragma mark -
#pragma mark AVAudioPlayer Delegate Methods

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)playedPlayer successfully:(BOOL)flag {
    if (releaseWhenDone) {
        NSLog(@"releasing");
        [playedPlayer release];
//      [self release];
        NSLog(@"released");
    }
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
    NSLog(@"Error while decoding: %@", [error localizedDescription] );
}

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
    NSLog(@"Interrupted!");
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player {
    NSLog(@"EndInterruption!");
}

- (BOOL) play;
{   
    player.currentTime = 0.0;
    return [player play];
}

注释掉 [player setDelegate: self]; 使错误消失,但我的 audioPlayerDidFinishPlaying 没有被调用。

有什么想法吗?我是否突然在另一个线程中运行?

4

1 回答 1

0

我发现了问题。我的错误,当然。

在我的很多类文件中,我添加了:

-(BOOL) respondsToSelector:(SEL) aSelector
{
    NSLog(@"Class: %@ subclass of %@, Selector: %@", [self class], [super class], NSStringFromSelector(aSelector));
    return [super respondsToSelector:aSelector];

}

主要是出于好奇。

好吧,当我向我的声音添加一个委托时,这个方法会在委托之前被调用,并且它会从 AVAudioPlayer 碰巧在的任何运行循环中调用,并且很可能是一个没有自动释放池的运行循环。

于 2009-06-20T18:59:36.430 回答