raghul
raghul

Reputation: 1048

AVAudioPlayer delegate wont get called in a class method?

i am using the AVAudioPlayer and setting its delegate but its delegate is not getting called

      + (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];
}

My class where i am implementing is the sound class

  @interface    SoundClass : NSObject <AVAudioPlayerDelegate>

I am calling this delegate

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

Upvotes: 3

Views: 5225

Answers (1)

Matt H
Matt H

Reputation: 6532

It looks like maybe your flip object is going out of scope, because the rest of your code looks fine. Here's what I do:

// 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;

Then allocate and load the sound in your .m

player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:Nil];
[player prepareToPlay];
player.delegate = self;
[player play];

Now you should get your DidFinishPlaying notification.

Upvotes: 1

Related Questions