Doug Null
Doug Null

Reputation: 8327

How to set a delegate to a class method

Can the AVAudioPlayer delegate be set to a class member audioPlayerDidFinishPlaying?

I want to use a class method to play a sound file, but can't figure out how to set setDelegate: to the audioPlayerDidFinishPlaying class method.

I have a small class called 'common' with just static members.

Please see '<<<<' flag below...

@class common;

@interface common : NSObject  <AVAudioPlayerDelegate> {
}
    +(void) play_AV_sound_file: (NSString *) sound_file_m4a;
    +(void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag 
@end

@implementation common

AVAudioPlayer* audioPlayer;

// Starts playing sound_file_m4a in the background.
+(void) play_AV_sound_file: (NSString *) sound_file_m4a
{
    printf("\n play_AV_sound_file '%s' ", [sound_file_m4a UTF8String] );

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@"m4a"]; 
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: soundPath] error:&error ];

    [audioPlayer   setDelegate:audioPlayerDidFinishPlaying];       //<<<<<<<<<< causes error
                  >>>  what should setDelegate: be set to?  <<<


    [audioPlayer   prepareToPlay];
    [audioPlayer   play];
}


+(void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag 
{
    printf("\n audioPlayerDidFinishPlaying");

    [audioPlayer release];
    audioPlayer=nil;
    [audioPlayer setDelegate:nil];    
}

@end

Upvotes: 1

Views: 2425

Answers (3)

basvk
basvk

Reputation: 4546

Like commented by Philip Mills, this is not how delegates work.

You could perform a quickfix (dirty) by changing the class method like so:

+(void) play_AV_sound_file:(NSString *)sound_file_m4a withDelegate:(id<AVAudioPlayerDelegate>)delegate;

And use the delegate parameter in your class method to forward it to your audioPlayer instance

Upvotes: 0

WDUK
WDUK

Reputation: 19030

That's not how delegates work.

You assign a class instance to be a delegate for another instance. Now in your case, this isn't easy, as a class method isn't part of an instance (it's static). As such, you'll need to make a Singleton, in order to produce one global instance for your class (which is equivalent to providing class methods).

To do so, make common a singleton by providing this as your only class method:

static common* singleCommon = nil;
+(common*) sharedInstance {
   @synchronized( singleCommon ) {
       if( !singleCommon ) {
           singleCommon = [[common alloc] init];
       }
   }

   return singleCommon;
}

From then in, in your example , you'd use.

[audioPlayer setDelegate:[common sharedInstance]];

In doing so, you need to make sure your common class (which ideally should have a capital C), has an instance method, that follows the AVAudioPlayDelegate protocol (which by the looks of it, it does for class methods). You'd need to change

+(void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag 

to

-(void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag 

In my opinion, having a singleton as a delegate for something isn't great design. In answer to your original question though, no, you can't assign class methods as individual delegates, you can only set instances of whole classes. I'd strongly suggest you read up on how delegation works: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18

Upvotes: 2

Phillip Mills
Phillip Mills

Reputation: 31016

Your best bet would be to use instance methods and create and actual object that belongs to your common class, then use self as the delegate.

(Technically, you might be able to get away with your current code and [audioPlayer setDelegate:(id<AVAudioPlayerDelegate>)[self class]]; to trigger the class method but it's not really a good idea even if it works.)

Upvotes: 0

Related Questions