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

Ios AVAudioPlayer委托不会在类方法中被调用?,ios,xcode,delegates,avaudioplayer,Ios,Xcode,Delegates,Avaudioplayer,我正在使用AVAudioPlayer并设置其委托,但未调用其委托 + (void) playflip { NSString *path; path = [[NSBundle mainBundle] pathForResource:@"flip" ofType:@"mp3"]; AVAudioPlayer *flip; flip = [[AVAudioPlayer alloc] initWithConten

我正在使用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];
}
我正在实现的类是sound类

  @interface    SoundClass : NSObject <AVAudioPlayerDelegate>

看起来您的
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;

现在你应该得到你的
DidFinishPlaying
通知了。

你刚才试过
flip.delegate=self?当我这样做的时候,我不进行强制转换。是的,我尝试了,但是没有被调用。虽然它显示了一些警告,它说从类中分配给AVAudioPlayerDelegate的不兼容指针类型虽然声音正在播放,但是委托只是没有被调用,但我正在类方法中使用它??错了吗??不是一个实例方法我唯一能看到的可能是您的
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;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:Nil];
[player prepareToPlay];
player.delegate = self;
[player play];