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 8915075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:53:18+00:00 2026-06-15T04:53:18+00:00

I have created a Singleton class to act as an audio player that will

  • 0

I have created a Singleton class to act as an audio player that will start the audio upon launch, and can be turned off from another class.

In the other class, when I call

[[Singleton singleton] audioPlayer stop];

It gives me the error

'Expected :'

I understand OOP but I don’t think I’ve ever tried to access an object of an object. Any ideas?

//  Singleton.m


#import "Singleton.h"

@implementation Singleton

#pragma mark Singleton Methods

+ (id)sharedManager {
static Singleton *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    singleton = [[self alloc] init];
});
return singleton;
}

- (id)init {
if (self = [super init]) {

    boy = false;
    girl = false;

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Mathletics Theme.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;
    audioPlayer.volume = 0.85f;

    if (audioPlayer == nil)
        NSLog([error description]);
    else
        [audioPlayer play];

    audioPlayer.numberOfLoops = -1;

    if (audioPlayer == nil)
        NSLog([error description]);
    else
        [audioPlayer play];
}
return self;
}

@end





//  Singleton.h

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface Singleton : NSObject {

AVAudioPlayer *audioPlayer;

}

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

+ (id)singleton;

@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-15T04:53:20+00:00Added an answer on June 15, 2026 at 4:53 am

    In your Singleton class, you may have the method like + (MySingleton *) instance

    So use [[Singleton instance] audioPlayer stop];

    or

    Singleton *singleton = [Singleton instance];
    [singleton.audioPlayer stop];
    

    Example MySingleton with NSString

    MySingleton *mySingleton = [MySingleton instance];
    mySingleton.myString = @"hi sigletone";    
    NSLog(@"%@",mySingleton.myString);
    

    Example MySingleton.h

    @interface MySingleton : NSObject
    
       @property (nonatomic,retain) NSString *myString;
    
      + (MySingleton *) instance;
    
    @end
    

    Example MySingleton.m

    #import "MySingleton.h"
    
    @implementation MySingleton
    
    - (id) initSingleton
    {
        if ((self = [super init]))
        {
            // Initialization code here.
        }
    
        return self;
    }
    
    + (MySingleton *) instance
    {
        // Persistent instance.
        static MySingleton *_default = nil;
    
        // Small optimization to avoid wasting time after the
        // singleton being initialized.
        if (_default != nil)
        {
            return _default;
        }
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
        // Allocates once with Grand Central Dispatch (GCD) routine.
        // It's thread safe.
        static dispatch_once_t safer;
        dispatch_once(&safer, ^(void)
                      {
                          _default = [[MySingleton alloc] initSingleton];
                      });
    #else
        // Allocates once using the old approach, it's slower.
        // It's thread safe.
        @synchronized([MySingleton class])
        {
            // The synchronized instruction will make sure,
            // that only one thread will access this point at a time.
            if (_default == nil)
            {
                _default = [[MySingleton alloc] initSingleton];
            }
        }
    #endif
        return _default;
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have singleton class that its object is not statically created. having
I have created a hashmap in a singleton class, we will call it Class
I have created a singleton class to keep track of my data on my
I have created some JQuery that will expand a div 'popup' on hover and
i created a singleton class and trying to access that class in other class
I have created a Registry class in .NET which is a singleton. Apparently this
I have a singleton class that I am using as part of a CAPTCHA
Using FluentNHibernate in a web application, I've created a singleton SessionFactory class to have
I have a java singleton class that has my application settings. I used this
Actually i have created an singleton class. Now my singleton class extends Activity, and

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.