当视图打开时,我有背景音乐,即使我将视图切换到其他视图并再次返回主视图,音乐开始完美播放也没有错误,背景音乐仍然没有问题。但是当我从后退按钮更改我的代码时
mainViewController *mainView =[[mainViewController alloc] initWithNibName:nil bundle:nil];
mainView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mainView animated:YES]; // this will flip from right
到
[self dismissModalViewControllerAnimated:YES]; // this will flip back from left
当翻到主菜单时,我的背景音乐没有播放。只是好奇为什么?怎么解决?由于某些原因我将代码更改为[self dismissModalViewControllerAnimated:YES];因为我希望我的观点从其他方式转回来。
当使用此代码显示mainView时,我的背景音乐正在播放
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *pathFile = [[NSBundle mainBundle] pathForResource:@"Start" ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathFile] error:NULL];
[player play];
}
如果视图发生变化则停止
- (void) viewDidDisappear:(BOOL)animated
{
[player stop];
}
答案 0 :(得分:0)
在ViewDidAppear方法中启动播放器。希望它能帮到你
-(void)viewDidAppear:(BOOL)animated
{
NSString *pathFile = [[NSBundle mainBundle] pathForResource:@"Start" ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathFile] error:NULL];
[player play];
[super viewDidAppear:YES];
}