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
atpietr
  • 17
  • 5
  • That's just a syntax error. Did you mean `[[Singleton singleton].audioPlayer stop]` or `[[Singleton audioPlayer] stop]`? – Thilo Nov 28 '12 at 08:13
  • 2
    You have singleton class named Singleton, and it has a class method named 'singleton', which return the singleton instance of the class? The instance has a property named audioPlayer? And the audioPlayer has a method called stop? Then you should call [[[Singleton singleton] audioPlayer] stop]; – Dave Nov 28 '12 at 08:14
  • @Dave it says "instance method '-audioPlayer' not found"... I can post my code to the Singleton class if that will help – atpietr Nov 28 '12 at 08:16
  • To reach your singleton instance you should call [Singleton sharedManager]. Where are declatating the audioPlayer property? – Dave Nov 28 '12 at 08:24
  • I declare the audioPlayer in Singleton.h – atpietr Nov 28 '12 at 08:25
  • As a property? Please provide that code too. – Dave Nov 28 '12 at 08:27
  • Ok, I forgot to synthesize the audio player, thank you. But how would I start the audio from launch? – atpietr Nov 28 '12 at 08:34
  • @property (nonatomic, strong) AVAudioPlayer* audioPlayer; – Dave Nov 28 '12 at 08:34
  • I had it in the AppDelegate before but then I couldn't access it from the other class. – atpietr Nov 28 '12 at 08:36
  • from application launch? in the application:didFinishLaunchingWitOptions: method of the appDelegate set an AVAudioPlayer instance to the singleton and call [[[Singleton singleton] audioPlayer] start] or something like this ( as I don't know your code). – Dave Nov 28 '12 at 08:37
  • Beautiful. Thank you so much, I really appreciate your help. This solved everything. – atpietr Nov 28 '12 at 08:42

2 Answers2

0

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
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
0

you miss brackets [ ]

[[[Singleton singleton] audioPlayer] stop];

but from your code this looks to be not the only problem

[[singleton sharedManager /*your method].audioPlayer stop];

audioPlayer has to be a property on the singleton to make the var available

@property(readonly) AVPlayer *audioPlayer;
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135