ViewController1.h
@interface ViewController : UIViewController
@property AVAudioPlayer *audioPlayer;
@end
ViewController1.m
@synthesize audioPlayer;
...
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[audioPlayer play];
如何停止ViewController2类中方法的音乐?
答案 0 :(得分:0)
使用NSNotification:
将此位置设置为用户在ViewController2中点击停止音乐:
[[NSNotificationCenter defaultCenter] postNotificationName:@"stopMusic" object:nil];
并且在ViewController1中应该有:
-(void)viewDidLoad:
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopTheMusic:) name:@"stopMusic" object:nil];
}
-(IBAction)stopTheMusic:(id)sender {
[audioPlayer stop];
}
祝你好运!