我有两个视图控制器:
MainMenuView:
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"BackgroundSound" ofType:@"mp3"];
play =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
play.delegate=self;
[play play];
}和PlayAreaView:
- (void)counttimer
{
///here is my code for NSTimer, if Timer is 0, Automatically I go to GameOver viewController
MainMenuView *vv = [[MainMenu alloc]init];
[vv.play stop];
GameOver *gv = [[GameOver alloc]init];
[self presentViewController:gv animated:YES completion:nil];
}在我转到GameOver之前,或者当我已经在游戏画面上时,我想从我的MainMenuView中停止AVAudioPlayer。请帮帮忙。我尝试分配MainMenuView,并使用[MainMenu.play stop],但不起作用。
发布于 2012-11-24 22:49:49
首先,您应该以稍微不同的方式命名变量。在代码中看到"[play play]“真的很奇怪。
其次,当我查看您的"counttimer“方法时,我看到的是您通过"alloc”和"init“创建了"MainMenuView”视图控制器,并在加载视图之前立即停止播放器。
如果您想要停止播放器,请确保您的"AVAudioPlayer“对象已实际加载。
因此,在您的代码中,执行如下操作:
if (vv.play)
[vv.play stop];
else
NSLog( @"vv.play is NULL, it doesn't exist yet");这将帮助您找到调用"stop“的合适位置。
https://stackoverflow.com/questions/13541546
复制相似问题