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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-01-2009, 10:57 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 48
Question Application Settings

How can I stop this music playing from another class file?
eg:

The music starts playing in the menu.h/.m

menu.h
---------
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>


@interface Menu : UIViewController {
	
	AVAudioPlayer* backgroundMusic;

}

@end
menu.m
---------
Code:
- (void)viewDidLoad {
	NSString *pathToMusicFile = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"mp3"];
	
	backgroundMusic = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile] error:NULL];
	
	backgroundMusic.numberOfLoops = -1;
	backgroundMusic.volume = 0.2;
	[backgroundMusic play];
    [super viewDidLoad];
}

Now that the song is playing, how can I stop it from this class?

options.h
-----------
Code:
@interface Options : UIViewController {
	
	IBOutlet UISwitch *musicSwitch;
	IBOutlet UISwitch *soundSwitch;

}

@property (nonatomic, retain) UISwitch *musicSwitch;
@property (nonatomic, retain) UISwitch *soundSwitch;
options.m
-----------
Code:
@synthesize musicSwitch;
@synthesize soundSwitch;

-(IBAction)musicSwitchChange:(id)sender
{
	if(musicSwitch.on)
	{
		
	}
	else
	{
		
	}
}
-(IBAction)soundSwitchChange:(id)sender
{
	if(soundSwitch.on)
	{
		
	}
	else
	{
		
	}
}
Thanks
__________________
Discover new apps through art! Sound weird? Check it out! BigAppAd!
Follow me on Twitter!
JavaWizKid is offline   Reply With Quote
Old 09-01-2009, 12:07 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Don't do that.

If you have background music, create a MusicController class to control it. Then send messages to the instance of that class from whatever object you're in. So instantiate the object in your appDelegate's appDidLoad, then send a message from your menu class object to start the music. Then you can send a message from your options class object to stop it.

joe
FlyingDiver is offline   Reply With Quote
Old 09-01-2009, 12:09 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 48
Default

Ok I see the structure but how do I do the actual code?
__________________
Discover new apps through art! Sound weird? Check it out! BigAppAd!
Follow me on Twitter!
JavaWizKid is offline   Reply With Quote
Old 09-01-2009, 12:11 PM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by JavaWizKid View Post
Ok I see the structure but how do I do the actual code?
What part don't you understand? I'm not going to write it for you....

joe
FlyingDiver is offline   Reply With Quote
Old 09-01-2009, 12:13 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 48
Default

I'm not expecting you to :P If the instance is created in one class, how can I control it from another?
__________________
Discover new apps through art! Sound weird? Check it out! BigAppAd!
Follow me on Twitter!
JavaWizKid is offline   Reply With Quote
Old 09-01-2009, 12:16 PM   #6 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by JavaWizKid View Post
I'm not expecting you to :P If the instance is created in one class, how can I control it from another?
By getting a reference (pointer) to that object. In this case, the musicPlayer object would normally be an instance variable of the AppDelegate class. So you use a property of the AppDelegate to get that reference and send the message directly. There's about a dozen threads a day in this forum about how to message one class object from another.

joe
FlyingDiver is offline   Reply With Quote
Old 09-01-2009, 02:10 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 48
Default

I can't find any code examples, can you point me in the right direction? Thanks
__________________
Discover new apps through art! Sound weird? Check it out! BigAppAd!
Follow me on Twitter!
JavaWizKid is offline   Reply With Quote
Reply

Bookmarks

Tags
class, iphone, music, xcode

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
» Online Users: 256
19 members and 237 guests
ADY, Alsahir, beleg_1998, Dani77, e2applets, iph_s, JamesCahall, JasonR, mer10, Monstertaco, piesia, prchn4christ, Promo Dispenser, Robiwan, Rudy, smithdale87, timle8n1, Touchmint
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,756
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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