I’ve searched multiple forums including this one, and cannot find a solution to my problem. I have a sound file set to play when my “IQTest” view controller loads. It plays fine, and I can stop the sound in “IQTest” view controller.
IQTest.h
@interface IQTest : UIViewController
{
AVAudioPlayer *theAudio;
}
@property (nonatomic, strong) AVAudioPlayer *theAudio;
@end
IQTest.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"IQTestBackgroundMusic" ofType:@"mp3"];
AVAudioPlayer* soundTrack=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
soundTrack.numberOfLoops = -1;
self.theAudio = soundTrack;
[theAudio play];
}
I want to stop playing the sound when a user presses the button in “IQTestQuestionThree”‘s view controller, but when I try at stop the sound, it either keeps playing, or my app crashes. I’ve tried multiple ways, and have yet to find one to stop my sound.
IQTestQuestionThree.m
- (IBAction) question3Answer1
{
IQTest *IQTestAudio = [[IQTest alloc] init];
[IQTestAudio.theAudio stop];
iqScaryFace.hidden = NO;
homeButton.hidden = NO;
homeButtonLabel.hidden = NO;
answer1Button.hidden = YES;
answer2Button.hidden = YES;
answer3Button.hidden = YES;
answer4Button.hidden = YES;
}
Thank you for any help I can get.
This code creates a brand new
IQTestobject (which is aUIViewControllersubclass — and therefore has a confusing name), and then calls itsstopmethod.This brand-new
IQTestobject (maybe rename itIQTestViewController?) was just created — it isn’t the same object that started playing the sound. So you’re expecting it to stop playing something it has no control over. (At least, I don’t expect it to work, without seeing more code.)How is the view hierarchy structured?
Is
IQTestQuestionThreea view controller whose parent isIQTest? (Again, consider renamingIQTestQuestionThreeto something likeIQTestQuestionThreeViewController.) If so, you could use the delegate pattern to haveIQTestQuestionThreealertIQTestto stop playing the sound.If your view controller hierarchy is more complex, and/or if there are other view controllers in other places that might want to stop playing the sound, consider creating a model class for sound playing to act as a proxy for playing sounds, instantiate that model class in your app delegate, and pass it along to all the view controllers that need to control audio.
(
UIViewControllersubclasses, by convention, have “ViewController” as a suffix on their name. You’ll find this is the case in Apple’s source code. If you want to write your code for readability, you should follow conventions for what language in which you’re writing as closely as possible.)