我在我的UIView中创建了一个MPMoviePlayerViewController(不在我的UIVIewController中!),如下所示:
self.playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 70, 125, 100)];
[self.playButton setBackgroundImage:[UIImage imageNamed:@"video_play.png"] forState:UIControlStateNormal];
[self.playButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
self.playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
self.playerViewController.moviePlayer.fullscreen = NO;
self.playerViewController.view.frame = CGRectMake(0, 0, 320, 200);
[self.playerViewController.moviePlayer prepareToPlay];
self.playerViewController.moviePlayer.shouldAutoplay = NO;
self.playerViewController.view.backgroundColor = [UIColor yellowColor];
[self addSubview:self.playerViewController.view];
[self.playerViewController.view addSubview:self.playButton];
}
- (void)buttonPressed:(id)sender{
(NSLog(@"Click"));
[self.playerViewController.moviePlayer setFullscreen:YES animated:YES];
[self.playerViewController.moviePlayer play];
}
正如你所看到我在videoView上添加了一个Button,因为这部分应该只是一个预览版,当用户点击按钮时,MPMoviePlayerViewController应该动画到全屏并开始播放,当视频完成时它应该去回到预览视图。到目前为止一切都有效,但我有两个问题:
第一个问题: 每次我打开View我的StatusBar都会被隐藏起来,它看起来像这样:
所以我在我的UIViewController的viewWillAppear和viewDidAppear中设置:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
这有效,但现在状态栏被隐藏并立即再次显示,看起来很难看,有没有机会解决这个问题?
第二个问题:
当我点击自定义按钮时,视频会全屏显示,一切正常!但当我按下视频的DONE按钮时,一切都会回到预览屏幕,它看起来像这样:StatusBar被隐藏,导航栏也被破坏,视频上方有很多黑色空间,这里有什么问题?
答案 0 :(得分:2)
好的,我找到了这个问题的解决方案,它有点hacky但我找不到其他解决方案。在我的ViewController中我做:
- (void)viewDidLoad {
[super viewDidLoad];
float delay = 0.1;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].statusBarHidden = NO;
});
对于用户点击按钮i的情况:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[UIApplication sharedApplication].statusBarHidden = NO;
}