I have firstViewController and SecondViewController. In both controllers I can tap play button and melody will play. So when I tap play button in 1 controller and then go to 2controller and tap other play button 2 melodies will play together. So my question is how to stop 1st melody from 1st controller when I tap play button in 2nd controller. Thank you!
In firstViewController viewdidload
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"first" ofType:@"wav"];
NSURL *soundURL_ = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *first = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL_ error:nil];
[first prepareToPlay];
The same in secondViewcontroller the same but second instead of first
When I pressed the button:
[first play]
You can create one instantiation of custom object that contains the AVAudioPlayer and methods associated with the player and then create a pointer for that object in both of your views.
If you would like to instantiate your AVAudioPlayer from within the AppDelegate you will have to first set up your AVAudioPlayer and assign it to a property of your AppDelegate.
From within your View Controllers, accessing any property of your App Delegate is easy, just write:
followed by:
Let me know if you’d like more detailed instructions.