Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How to reference a music background from a singleton class?

AminkAmink Posts: 2New Users
I have done a singleton class called MyBgMusic.h & MyBgMusic.m. How to reference that singleton class to my SecondViewController or the rest of the XIB.

H file :

#import
#import

@interface MyBgMusic : UIViewController {


AVAudioPlayer *player;
UIButton *playBgMusic;


}

@property (nonatomic, retain) IBOutlet AVAudioPlayer *player;
@property (nonatomic, retain) IBOutlet UIButton *playBgMusic;


+ (id)sharedManager;

-(IBAction) toggleMusic;


@end

M file :

#import "MyBgMusic.h"

static MyBgMusic *sharedMyManager = nil;

@implementation MyBgMusic

@synthesize player,playBgMusic;

#pragma mark -
#pragma mark Singleton Methods

+ (MyBgMusic*)sharedManager {

static MyBgMusic *sharedMyManager;
if(!sharedMyManager) {
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedMyManager = [[super allocWithZone:nil] init];
});
}

return sharedMyManager;
}

+ (id)allocWithZone:(NSZone *)zone {

return [self sharedManager];
}


- (id)copyWithZone:(NSZone *)zone {
return self;
}

#if (!__has_feature(objc_arc))

- (id)retain {

return self;
}

- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}


- (id)autorelease {

return self;
}

- (void)dealloc
{
[MyBgMusic release];
[playBgMusic release];
[player release];
[super dealloc];
}
#endif

#pragma mark -
#pragma mark Custom Methods

- (IBAction)toggleMusic {

if ([self.player isPlaying] == YES) {
[self.player stop];
} else {
[self.player play];
}
self.playBgMusic.enabled = YES;

}

- (void)viewDidLoad
{

NSString *path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
player.delegate = self;
[player play];
player.numberOfLoops = -1;
[super viewDidLoad];

// Do any additional setup after loading the view from its nib.
}

SecondViewController.m ( I want to reference from the singleton class so that I can use it all over again without mess up the background music when pressing on and off. )

- (IBAction)toggleMusic{

if ([self.player isPlaying] == YES) {
[self.player stop];
} else {
[self.player play];
}
self.playBgMusic.enabled = YES;

}
Sign In or Register to comment.