Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:47:50+00:00 2026-06-13T20:47:50+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:47:52+00:00Added an answer on June 13, 2026 at 8:47 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use AVAudioPlayer to play sounds in my app, but it turns off the
Alright I have two problems. I'm using AVAudioPlayer to play a simple audio file
Hi I want to play a mp3 file via my server e.g. http://test.com/hi.mp3 At
How can I get an ipod library music file into AVAudioPlayer?
I have an audio file that plays using avaudioplayer, I want to able to
I'm trying to play a mp3 file using AVFoundation framework. It can't detect the
How can you find out if a AVAudioPlayer finished playing a sound inside of
Is it possible to use AVAudioPlayer to play custom sounds (e.g. I will generate
I want to loop a recorded sound 3 times, but want one second of
I'm new to the iPhone-Development. I want to play a Sound when an UIButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.