03-20-2011, 03:03 PM
#1 (permalink )
Registered Member
Join Date: Mar 2011
Posts: 6
Iphone xcode stop background music
Hello,
I added the following code in my .m file :
- (void)viewDidLoad
{
imageA setHidden:YES;
scrollView setScrollEnabled:YES;
scrollView setContentSize:CGSizeMake(320, 3420);
NSString *path = [NSBundle mainBundle] pathForResource:@"MyMusic" ofType:@"mp3";
AVAudioPlayer* musicLoop=[AVAudioPlayer alloc initWithContentsOfURL:NSURL fileURLWithPath ath] error:NULL;
//musicLoop.delegate = self;
musicLoop.numberOfLoops = -1;
musicLoop.volume = 0.5;
musicLoop play;
super viewDidLoad;
}
it's a background music, how can I add a button to stop it ? because each time I'm trying to do that,
xcode says:
musicLoop stop; Use of undeclared identifier 'musicLoop'
any help please ??
03-20-2011, 03:41 PM
#2 (permalink )
Registered Member
Join Date: Mar 2011
Posts: 128
Could you not in your .h file put this... (with your Outlets)
Code:
AVAudioPlayer* musicLoop;
then change this:
Code:
AVAudioPlayer* musicLoop=[AVAudioPlayer alloc initWithContentsOfURL:NSURL fileURLWithPath:path] error:NULL;
for
Code:
musicLoop=[AVAudioPlayer alloc initWithContentsOfURL:NSURL fileURLWithPath:path] error:NULL;
then where ever you needed to stop the music put
I think that should work. Hope that helps!
03-21-2011, 04:23 PM
#3 (permalink )
Registered Member
Join Date: Mar 2011
Posts: 6
thank you very much, but I have another problem now, I don't know why my simple application CRASH/ stopped after clicking on the button ( LetterA ) please find below the code:
------------------------ controller.h ---------------------------------
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ABC2ViewController : UIViewController <AVAudioPlayerDelegate> {
IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *imageA;
IBOutlet UIImageView *imageB;
AVAudioPlayer* musicLoop;
AVAudioPlayer* AudioC;
AVAudioPlayer* AudioB;
}
- (IBAction)letterA:(id)sender;
- (IBAction)letterB:(id)sender;
- (IBAction)musicStop:(id)sender;
@property(nonatomic, retain) IBOutlet UIImageView *imageA;
@property(nonatomic, retain) IBOutlet UIImageView *imageB;
@end
------------------------ controller.m ------------------------------------
Code:
[COLOR="rgb(160, 82, 45)"]#import "ABC2ViewController.h"
#import <AVFoundation/AVFoundation.h>
@implementation ABC2ViewController
@synthesize imageA;
@synthesize imageB;
- (IBAction)letterA:(id)sender{
NSString *path = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"m4a"];
AVAudioPlayer* AudioC=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//[imageA setHidden:NO];
if (imageA.hidden == TRUE) {
[imageA setHidden:FALSE];
[AudioC play];
//[self performSelector:@selector(hideagain)withObject:nil afterDelay:2];
}
else {
[imageA setHidden:TRUE];
[AudioC play];
}
}
- (IBAction)letterB:(id)sender{
NSString *path = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"m4a"];
AVAudioPlayer* AudioB=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
AudioB.delegate = self;
//[imageB setHidden:NO];
if (imageB.hidden == TRUE) {
[imageB setHidden:FALSE];
[AudioB play];
//[self performSelector:@selector(hideagain)withObject:nil afterDelay:2];
}
else {
[imageB setHidden:TRUE];
[AudioB play];
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[imageA setHidden:YES];
[imageB setHidden:YES];
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 3420)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"MusicLoop3a3" ofType:@"mp3"];
AVAudioPlayer* musicLoop=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
musicLoop.delegate = self;
musicLoop.numberOfLoops = -1;
musicLoop.volume = 0.5;
[musicLoop play];
[super viewDidLoad];
}
- (IBAction)musicStop:(id)sender{
[musicLoop stop];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc
{
[musicLoop release];
[musicLoop release];
[AudioC release];
[AudioB release];
[super dealloc];
}
@end
[/color]
-------------------------------
Can you help me please ? knowing that I get the following yellow error :
Local declaration of 'Audio' hides instance variable
03-21-2011, 04:41 PM
#4 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
This line:
Code:
AVAudioPlayer* AudioC=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
Should be
Code:
AudioC=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
If you declare something in the .h, you dont need the identifier before the variable. In this case it was AVAudioPlayer.
03-21-2011, 05:09 PM
#5 (permalink )
Registered Member
Join Date: Mar 2011
Posts: 6
Quote:
Originally Posted by
Objective Zero
This line:
Code:
AVAudioPlayer* AudioC=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
Should be
Code:
AudioC=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
If you declare something in the .h, you dont need the identifier before the variable. In this case it was AVAudioPlayer.
thank you very much for you help, but the application crash when I click on (letterA) and give me the following note in green:
[AudioC play]; Thread1: Stopped ay breakpoint1
03-21-2011, 05:12 PM
#6 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
Go to Build and Run - Breakpoints Off and click on that.
03-21-2011, 05:18 PM
#7 (permalink )
Registered Member
Join Date: Mar 2011
Posts: 6
Hello, I'm using xcode4, so just give me few minutes to find it please.
03-21-2011, 05:44 PM
#8 (permalink )
Registered Member
Join Date: Mar 2011
Posts: 6
Quote:
Originally Posted by
Objective Zero
Go to Build and Run - Breakpoints Off and click on that.
Hello
SOrry I tried but I don't know where Build and Run in xcode4 to do what you asked me to do
03-22-2011, 05:01 AM
#9 (permalink )
Registered Member
Join Date: Mar 2011
Posts: 6
Hello,
Everything work fine now, thanks, just one more question, about the last part of my .m file, is the following code is correct ?
- (void)dealloc
{
[musicLoop release];
[musicLoop release];
[AudioC release];
[AudioB release];
[super dealloc];
}
I don't have any error, but just to be sure that it's correct ?
thanks again for your help.
01-30-2012, 12:16 AM
#10 (permalink )
Registered Member
Join Date: Nov 2011
Posts: 227
Thank you Objective ZERO.
I thank you... beyond measure
if you ever need help with stuff I will repay the favour.
Last edited by 2WeeksToGo; 01-30-2012 at 12:18 AM .
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 348
10 members and 338 guests
condor304 , Creativ , Domele , dreamdash3 , laureix68 , LEARN2MAKE , michelle , mistergreen2011 , Sami Gh , tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,661
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, tinamm64