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 03-20-2011, 03:03 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 6
bbparis is on a distinguished road
Default 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 fileURLWithPathath] 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 ??
bbparis is offline   Reply With Quote
Old 03-20-2011, 03:41 PM   #2 (permalink)
Registered Member
 
Joe-Kinglake's Avatar
 
Join Date: Mar 2011
Posts: 128
Joe-Kinglake is on a distinguished road
Default

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

Code:
[musicLoop stop];
I think that should work. Hope that helps!
__________________
--------------------------------------------
Our Site
http://www.iworldapps.net/

Like Us On Facebook
http://on.fb.me/fcxiIZ



--------------------------------------------
Joe-Kinglake is offline   Reply With Quote
Old 03-21-2011, 04:23 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 6
bbparis is on a distinguished road
Default

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
bbparis is offline   Reply With Quote
Old 03-21-2011, 04:41 PM   #4 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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.
Objective Zero is offline   Reply With Quote
Old 03-21-2011, 05:09 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 6
bbparis is on a distinguished road
Default

Quote:
Originally Posted by Objective Zero View Post
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
bbparis is offline   Reply With Quote
Old 03-21-2011, 05:12 PM   #6 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Go to Build and Run - Breakpoints Off and click on that.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 03-21-2011, 05:18 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 6
bbparis is on a distinguished road
Default

Hello, I'm using xcode4, so just give me few minutes to find it please.
bbparis is offline   Reply With Quote
Old 03-21-2011, 05:44 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 6
bbparis is on a distinguished road
Default

Quote:
Originally Posted by Objective Zero View Post
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
bbparis is offline   Reply With Quote
Old 03-22-2011, 05:01 AM   #9 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 6
bbparis is on a distinguished road
Default

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.
bbparis is offline   Reply With Quote
Old 01-30-2012, 12:16 AM   #10 (permalink)
Registered Member
 
2WeeksToGo's Avatar
 
Join Date: Nov 2011
Posts: 227
2WeeksToGo is on a distinguished road
Default

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.
2WeeksToGo is offline   Reply With Quote
Reply

Bookmarks

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: 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
Powered by vBadvanced CMPS v3.1.0

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