0

当视图打开时我有背景音乐,然后音乐开始完美播放,即使我将视图切换到其他视图并再次返回主视图,背景音乐仍然可以正常播放。但是当我在后退按钮中更改我的代码时

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

1 回答 1

0

在 ViewDidApper 方法中启动您的播放器。希望对你有帮助

-(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];
}
于 2012-07-18T12:15:43.017 回答