Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 12-10-2011, 05:02 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 2
Amink is on a distinguished road
Default How to reference a music background from a singleton class?

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 <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface MyBgMusic : UIViewController <AVAudioPlayerDelegate> {


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)allocWithZoneNSZone *)zone {

return [self sharedManager];
}


- (id)copyWithZoneNSZone *)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 fileURLWithPathath] 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;

}
Amink is offline   Reply With Quote
Reply

Bookmarks

Tags
ios5, singleton class, singletonsoundmanager

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0