Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hodgey87

Guest
Original poster
Mar 20, 2009
41
0
Hi everyone,

Im having some trouble with AVAudioPlayer, i have 2 views in my application and some background music that runs at the same time. I want to be able to stop the music on the second view only but not sure how to do it.

Ive only been doing this for a few weeks so be easy on me :)

Cheers
 
On my first view i have

.h

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


@interface countdownViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}

- (IBAction)stop;

@end

.m

#import "countdownViewController.h"
#import "secondViewController.h"
#import <AudioToolbox/AudioServices.h>

@implementation countdownViewController

- (IBAction)stop{
[theAudio stop];
}

-(IBAction)infoButtonPressed:(id)sender{

secondViewController *second = [[secondViewController alloc] initWithNibName:nil bundle:nil];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:second animated:YES];

}

- (void)viewDidLoad {
[super viewDidLoad];
[super viewDidLoad];


NSString *filePath = [[NSBundle mainBundle] pathForResource:mad:"music" ofType:mad:"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

[theAudio play];
theAudio.numberOfLoops = -1;

[filePath release];
[fileURL release];

}

second view

.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "countdownViewController.h"

@interface secondViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}

- (IBAction)backButtonPressed:(id)sender;


- (IBAction)stop;

@end

.m

#import "secondViewController.h"
#import <AudioToolbox/AudioServices.h>


@implementation secondViewController


- (IBAction)backButtonPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];

}


- (IBAction)stop{
[theAudio stop];
}




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];


}


- (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.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end


I can stop it fine if its just on the 1st view, but not sure about on the second one.

Its probably something simple, but im not sure where to look.
 
where do i need to add somethin in? is it the view did load method something like.

NSString *filePath = [[NSBundle mainBundle] pathForResource:mad:"jingle-bells" ofType:mad:"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

[theAudio play];
theAudio.numberOfLoops = -1;

but that just plays 2 songs. Ive got the AVAudioPlayer *theAudio on the .h

Is it something in the backbuttonpressed method?

secondViewController *second = [[secondViewController alloc] initWithNibName:nil bundle:nil];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:second animated:YES];
 
Since you're new to this, let me ask: Have you encountered anything about sharing objects between classes in your learning so far?
 
I havent yet, do i need to add extra code into the appdelegate or something like that?
 
I havent yet, do i need to add extra code into the appdelegate or something like that?
That's one way to do it (although not really recommended). Another way is to make secondViewController's theAudio a property and then set it to refer to countdownViewController's theAudio before you present the modal view.
 
Except I said to make secondViewController's theAudio a property.

P.S. The passTheAudio method is unnecessary and not part of creating a property.
 
this is what i have then

.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "countdownViewController.h"

@interface secondViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}


@property (nonatomic,retain) AVAudioPlayer *theAudio;

- (IBAction)backButtonPressed:(id)sender;


- (IBAction)stop;

@end

.m

#import "secondViewController.h"
#import <AudioToolbox/AudioServices.h>
#import "countdownViewController.h"


@implementation secondViewController

@synthesize theAudio;


- (IBAction)backButtonPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];

countdownViewController *count = [[countdownViewController alloc] initWithNibName:nil bundle:nil];
count.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:count animated:YES];

}


- (IBAction)stop{
[theAudio stop];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];


}


- (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.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end

anything else i need to add at all?
 
anything else i need to add at all?
First, be a good memory-citizen and don't forget to release your property's ivar in the dealloc.

Second, since theAudio is now a property, you should use the getter when referring to it.

Third, don't forget to set the value of theAudio property from the secondViewController instance before your present the view for it (i.e. a change in countdownViewController is still needed).
 
i dont understand the last bit though :eek:
The reason we created a property for theAudio in secondViewController is so that you could set it once you have an instance of secondViewController (which, in your case, is named second). This, then, permits both classes to point to the same object. But, the key is: you need to assign the property a value, after you instantiate secondViewController but before you present second's view.

P.S. Just a word of advice: Normal naming convention is to start class names with an upper-case character (as in, SecondViewController) rather than a lower-case character (as in, secondViewController) so as to differentiate the class names from the variable names.
 
This is slightly confusing now, i cant find many examples on what to do here. Any chance of something example coding?
 
This is slightly confusing now, i cant find many examples on what to do here. Any chance of something example coding?
Here's an example of setting a property of an instance to a value (from your own code!):
Code:
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
second is the instance.
modalTransitionStyle is the property.
UIModalTransitionStyleFlipHorizontal is the value.

If this seems confusing, perhaps it's time to step back from the real coding and go (re)learn the basics of Objective-C programming. Speaking of which, what/where have you learned from already?
 
Cheers i know its frustrating :) I'm currently learning at university now, my books arrived today.

ive added this code

second.theAudio = theAudio;

seems to work.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.