0

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 类中的方法中停止音乐?

4

1 回答 1

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];
}

祝你好运!

于 2012-07-29T18:36:06.147 回答