AVAudioPlayer委托不会在类方法中调用?

时间:2011-12-01 15:00:53

标签: ios xcode delegates avaudioplayer

我正在使用AVAudioPlayer并设置其委托但其委托不会被调用

      + (void) playflip
        {
        NSString *path;
        path = [[NSBundle mainBundle] pathForResource:@"flip" ofType:@"mp3"];
        AVAudioPlayer *flip;
        flip = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:Nil];
        flip.delegate = self;
        [flip play];
}

我正在实施的课程是声音课程

  @interface    SoundClass : NSObject <AVAudioPlayerDelegate>

我打电话给这个代表

    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"delegate called");
    [player release];
    player = nil;
}

1 个答案:

答案 0 :(得分:1)

看起来您的flip对象可能超出范围,因为其余代码看起来很好。这是我的所作所为:

// controller.h
@interface    SoundClass : NSObject <AVAudioPlayerDelegate> {}
// @property(nonatomic,retain) NSMutableDictionary *sounds;
// I have lots of sounds, pre-loaded in a dictionary so that I can reference by name
// With one player, you can just use:
@property(nonatomic,retain) AVAudioPlayer *player;

然后在.m

中分配并加载声音
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:Nil];
[player prepareToPlay];
player.delegate = self;
[player play];

现在您应该收到DidFinishPlaying通知。