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 に答える