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 Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2010, 09:25 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default Make AVAudioPlayer stop playing in a different View Controller?

Hi. I recently created an app that has background music playing (and looping) through an AVAudioPlayer. I start it when the 1st view controller is finished loading. What happens is the user clicks a button on the 1st view controller (loading screen) then it switches to the second view controller (main menu) and the music keeps looping uninterrupted. The user then clicks on a start game button and it switches to the 3rd view controller (the game). I want it to stop on the 3rd view controller (which again is the actual game). Here's the code I used:

1st view controller header:
Code:
@interface LoadScreen : UIViewController {
	AVAudioPlayer *backgroundMusic;
}
@end
1st view controller main:
Code:
- (void)changeLoadingTextToTapToStart {	
	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 = 2.0;
}
The function changeLoadingTextToTapToStart is set up on a timer and set to activate when the view finishes loading. How would I stop the looping and sound altogether in the 3rd view controller?
Whitehk is offline   Reply With Quote
Old 05-13-2010, 05:23 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 recently created an app that has background music playing (and looping) through an AVAudioPlayer. I start it when the 1st view controller is finished loading. What happens is the user clicks a button on the 1st view controller (loading screen) then it switches to the second view controller (main menu) and the music keeps looping uninterrupted. The user then clicks on a start game button and it switches to the 3rd view controller (the game). I want it to stop on the 3rd view controller (which again is the actual game). Here's the code I used:

1st view controller header:
Code:
@interface LoadScreen : UIViewController {
	AVAudioPlayer *backgroundMusic;
}
@end
1st view controller main:
Code:
- (void)changeLoadingTextToTapToStart {	
	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 = 2.0;
}
The function changeLoadingTextToTapToStart is set up on a timer and set to activate when the view finishes loading. How would I stop the looping and sound altogether in the 3rd view controller?
Anyone?
Whitehk is offline   Reply With Quote
Old 05-13-2010, 05:32 PM   #3 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

There's way too many possible answers for anyone to give you. You could pass an object pointer from one view controller to another, put the AVAudioPlayer in your app delegate, or inside of a singleton class. I've seen posts in the forum arguing for each of the above, with no decision on which is best.

I would recommend you search this forum, and Google a bit, and pick one of these approaches. Then if you have trouble making it work, post back here with questions.
JasonR is offline   Reply With Quote
Old 05-13-2010, 08:42 PM   #4 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by JasonR View Post
There's way too many possible answers for anyone to give you. You could pass an object pointer from one view controller to another, put the AVAudioPlayer in your app delegate, or inside of a singleton class. I've seen posts in the forum arguing for each of the above, with no decision on which is best.

I would recommend you search this forum, and Google a bit, and pick one of these approaches. Then if you have trouble making it work, post back here with questions.
Thanks for the great foundation to work off of. I will definitely post back here if I have any further questions.
Whitehk is offline   Reply With Quote
Old 06-11-2011, 11:32 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 1
bendigi is on a distinguished road
Default Curious

Quote:
Originally Posted by Whitehk View Post
Thanks for the great foundation to work off of. I will definitely post back here if I have any further questions.
Any luck ?
bendigi is offline   Reply With Quote
Reply

Bookmarks

Tags
avaudioplayer, avaudioplayer stop, background music, stop sound, 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: 315
14 members and 301 guests
Abidullah, baja_yu, cgokey, Duncan C, Fstuff, gbenna, guusleijsten, jbro, mdpauley, n00b, newDev, Sami Gh, seokwon lee, Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,113
Posts: 402,877
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

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