Note: I have looked at other questions and the answers to them are quite vague or unhelpful
I have this code in View_Controller.h
@property AVAudioPlayer *playerSaxophone;
Then I do this in the same file (in viewDidLoad
):
NSURL *backgroundMusicSaxophone = [NSURL fileURLWithPath:
[[NSBundle mainBundle]
pathForResource:@"saxophone" ofType:@"wav"]];
self.playerSaxophone = [[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundMusicSaxophone error:nil];
self.playerSaxophone.numberOfLoops = -1;
[self.playerSaxophone setVolume:0.5];
[self.playerSaxophone play];
In a different view controller I want to be able to stop or start this audio from playing by clicking 2 buttons. Is there any way I can do this?
Edit: I tried this in the "different" view controller .m file
//I do import ViewController.h in this file
- (IBAction)stop:(id)sender {
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[viewController.playerSaxophone stop];
}
But it didn't work.