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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-29-2010, 09:30 AM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default Start AVAudioPlayer in App Delegate and make it available to all view controllers?

Hi. I am trying to figure out how to make an AVAudioPlayer controllable (play, stop) throughout my app. I am doing this by starting it in my app delegate. The only thing is, I still get errors when I try to stop it in a different view controller. Here's how I did it:

MyAppDelegate.h:
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "MainMenu.h"

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
	UIWindow *window;
	MainMenu *viewController;
	
	AVAudioPlayer *backgroundMusic;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MainMenu *viewController;

@end
MyAppDelegate.m:
Code:
#import "MyAppDelegate.h"
#import "MainMenu.h"

@implementation MyAppDelegate

@synthesize window;
@synthesize viewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
	
	viewController = [[MainMenu alloc] init];
	// Override point for customization after app launch    
	[window addSubview:[viewController view]];
	
	[window makeKeyAndVisible];
	
	NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource: @"backgroundmusic" ofType: @"wav"];
	NSURL *backgroundMusicURL = [[NSURL alloc] initFileURLWithPath:backgroundMusicPath];
	backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
	[backgroundMusic play];
	backgroundMusic.numberOfLoops = -1;
	
	backgroundMusic.volume = 0.5;
}


- (void)dealloc {
	[viewController release];
	[window release];
	[super dealloc];
}


@end
------------------------
I want the backgroundMusic to play in the Main Menu (which, you could probably see, appears when the user starts the app, so that's why I started playing it in the delegate). However, when the user actually starts playing the game, I want it to stop (which is in a different view controller named level1). I get the variable by just doing #import MyAppDelegate.h into the level1.m file and did the normal [backgroundMusic stop]; to stop the music in the viewDidLoad method. However, backgroundMusic is still undeclared. I am at a complete loss. Sorry for this completely noob question. Thanks for the help in advance!
Whitehk is offline   Reply With Quote
Old 05-31-2010, 05:38 PM   #2 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
Hi. I am trying to figure out how to make an AVAudioPlayer controllable (play, stop) throughout my app. I am doing this by starting it in my app delegate. The only thing is, I still get errors when I try to stop it in a different view controller. Here's how I did it:

MyAppDelegate.h:
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "MainMenu.h"

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
	UIWindow *window;
	MainMenu *viewController;
	
	AVAudioPlayer *backgroundMusic;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MainMenu *viewController;

@end
MyAppDelegate.m:
Code:
#import "MyAppDelegate.h"
#import "MainMenu.h"

@implementation MyAppDelegate

@synthesize window;
@synthesize viewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
	
	viewController = [[MainMenu alloc] init];
	// Override point for customization after app launch    
	[window addSubview:[viewController view]];
	
	[window makeKeyAndVisible];
	
	NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource: @"backgroundmusic" ofType: @"wav"];
	NSURL *backgroundMusicURL = [[NSURL alloc] initFileURLWithPath:backgroundMusicPath];
	backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
	[backgroundMusic play];
	backgroundMusic.numberOfLoops = -1;
	
	backgroundMusic.volume = 0.5;
}


- (void)dealloc {
	[viewController release];
	[window release];
	[super dealloc];
}


@end
------------------------
I want the backgroundMusic to play in the Main Menu (which, you could probably see, appears when the user starts the app, so that's why I started playing it in the delegate). However, when the user actually starts playing the game, I want it to stop (which is in a different view controller named level1). I get the variable by just doing #import MyAppDelegate.h into the level1.m file and did the normal [backgroundMusic stop]; to stop the music in the viewDidLoad method. However, backgroundMusic is still undeclared. I am at a complete loss. Sorry for this completely noob question. Thanks for the help in advance!
Anyone?
Whitehk is offline   Reply With Quote
Old 10-10-2010, 04:10 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 1
b33rad is on a distinguished road
Default

This post is a little old but for future others:

Code:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
//and then access the variable by appDelegate.variable
So in your case, replace 'YourAppDelegate' with the name of your app delegate and replace 'appDelegate.variable' with 'appDelegate.backgroundMusic' . I am pretty sure that will work.
Don't forget to set backgroundMusic as a property.


Here is the link I found it at. Referencing variables in App Delegate

Referencing AppDelegate instance variables - Stack Overflow

Last edited by b33rad; 10-10-2010 at 04:50 PM. Reason: Minor mistake
b33rad is offline   Reply With Quote
Reply

Bookmarks

Tags
appdelegate, avaudioplayer, global variables, noob, viewcontrollers

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: 405
10 members and 395 guests
7twenty7, ChrisYates, djohnson, Duncan C, gmarro, Kirkout, Retouchable, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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