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 03-18-2010, 10:50 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 118
Default How do I stop a sound using a button set to play in viewDidLoad?

Hi. I'm trying to play background music for my game, and I want the user to be able to mute the sound by pressing a button. The only problem with this is that I made the sound start playing (and looping) in the viewDidLoad method in my ViewController.m file. I set up an IBAction to stop the sound, but obviously I can't put an IBAction in my viewDidLoad method and have it work correctly. If I put it outside of the viewDidLoad method though, it will give me an error saying that backgroundMusic is undeclared. Here are some snippets of my code to give you a clearer picture:

my viewDidLoad method:

- (void)viewDidLoad {
[super viewDidLoad];
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource: @"Xtreme Pong Background" ofType: @"wav"];
NSURL *backgroundMusicURL = [[NSURL alloc] initFileURLWithPath:backgroundMusicPath];
AVAudioPlayer *backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
[backgroundMusic play];
backgroundMusic.numberOfLoops = -1;
}

My IBAction used to stop the sound:

-(IBAction)muteButton:(UIButton *)button; {
[backgroundMusic stop];
}
_________________________________________
If there's anyway I could start the sound at the app's startup and be able to mute it with a button, I'm open for ideas. Thanks!
Whitehk is offline   Reply With Quote
Old 03-18-2010, 10:56 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 253
Default

I did this in my game.
I had a screen with 3 buttons for "new game" "how to play" and "story".
I loaded a background music(in the code below) in my viewDidLoad
and simply stopped it as part of the IBAction associated with each of
the buttons as you mention that you tried to do.
It worked fine for me.







NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/western.wav", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];

}
davejas69 is offline   Reply With Quote
Old 03-18-2010, 11:08 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 118
Default

Quote:
Originally Posted by davejas69 View Post
I did this in my game.
I had a screen with 3 buttons for "new game" "how to play" and "story".
I loaded a background music(in the code below) in my viewDidLoad
and simply stopped it as part of the IBAction associated with each of
the buttons as you mention that you tried to do.
It worked fine for me.







NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/western.wav", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];

}
Like you said, I have tried this. If I put the IBAction in my ViewDidLoad method it simply doesn't work. If I put it outside of it, it says that backgroundMusic is undeclared. I guess my new question would be: Why would it be saying that backgroundMusic is undeclared?
Whitehk is offline   Reply With Quote
Old 03-19-2010, 12:11 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 253
Default

Ok well I can review what I did to make mine work, maybe you can recreate it...
in .h ....

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioServices.h>

@interface GameshellViewController : UIViewController {
IBOutlet UIButton *newGameButton;
IBOutlet UIButton *howToPlayButton;
IBOutlet UIButton *storyButton;

AVAudioPlayer *audioPlayer;
}
-(IBAction) newGameButtonActionid) sender;
-(IBAction) howToPlayButtonActionid) sender;
-(IBAction) storyButtonActionid) sender;





@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

@property(nonatomic,retain) IBOutlet UIButton *newGameButton;
@property(nonatomic,retain) IBOutlet UIButton *howToPlayButton;
@property(nonatomic,retain) IBOutlet UIButton *storyButton;



-(void)myclick2;

@end

////////////////////////////////////////////////////////////////////////////
Then you have to import the AudioToolbox .framework and remember NOT to check
the box that asks to "copy items into destination group folder"

In your .m file, synthesize your audioPlayer.
Beyond checking those things, not sure what else it can be.
Hope it helped.
davejas69 is offline   Reply With Quote
Reply

Bookmarks

Tags
avaudioplayer, button, mute, sound, stop sound

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: 262
18 members and 244 guests
14DEV, @sandris, ADY, ArtieFufkin10, ckgni, Dani77, HemiMG, IphoneSdk, jakerocheleau, JasonR, MACralik, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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