我有一个由两半组成的UIViewController(它们实际上是在UIContainerViews中)。 1个半部分包含UIWebView,用于显示PDF,另一半包含用于流式传输视频的UIView。
带有UIWebView的导航栏,带有UIButton,按下时会以全屏模式呈现模态UIWebview:
- (IBAction)pressedFullscreenButton:(id)sender
{
// pause the video view controller
self.videoViewController.videoPlayer pause];
// perform the segue - this is modal, full screen
[self performSegueWithIdentifier:@"FullscreenSegue" sender:self];
}
所以我按全屏按钮,视频暂停,我的pdf / uiwebview以模态全屏显示。当我关闭这个全屏视图控制器以回到我的分屏时,当我按下视频播放时,音频播放,但视频没有,我不能为生活弄清楚为什么 - 任何帮助将非常感激。
为了解决这个问题,这不是我的首选解决方案,我基本上可以获得视频的当前时间,停止它,设置当前时间,播放视频并在视图控制器出现时立即暂停。这是我的代码:
所以我使用了一个MPMoviePlayerController,它已经包含在一个名为StandardVideoPlayer的类中:
@implementation StandardVideoPlayer
-(id)initWithParentView:(UIView *)parent andAutoPlay:(BOOL)autoPlay andFullScreen:(BOOL)fullScreen andAnimated:(BOOL)animated
{
// Call superclass's initializer
self = [super init];
if( !self || !parent)
{
return nil;
}
else
{
if (!_moviePlayerController)
{
_moviePlayerController = [[MPMoviePlayerController alloc] init];
}
_moviePlayerController.controlStyle = MPMovieControlStyleDefault;
[_moviePlayerController.view setFrame:parent.frame];
[_moviePlayerController.view setCenter:parent.center];
_moviePlayerController.allowsAirPlay = YES;
_moviePlayerController.shouldAutoplay = autoPlay;
[_moviePlayerController setFullscreen:fullScreen animated:animated];
[parent addSubview: [self videoView]];
}
return self;
}
-(void)loadFromFile: (NSString*) filePath;
{
if (_moviePlayerController)
{
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
[_moviePlayerController setContentURL:fileUrl];
[_moviePlayerController prepareToPlay];
}
}
- (void)loadFromURL: (NSString*) urlString
{
if ( [[ConnectionManager sharedInstance] canConnect] )
{
if (_moviePlayerController)
{
NSURL *url = [NSURL URLWithString:urlString];
[_moviePlayerController setContentURL:url];
[_moviePlayerController prepareToPlay];
}
}
else
{
[[ConnectionManager sharedInstance] displayConnectionWarningWithText:@"Ok" andDelegate:nil];
}
}
-(void)loadFromHTML:(NSString *)html
{
// NSString *h = [NSString stringWithFormat:html, ]
}
-(void)play
{
if (_moviePlayerController)
{
[_moviePlayerController prepareToPlay];
[_moviePlayerController play];
}
}
-(void)stop
{
if (_moviePlayerController)
{
[_moviePlayerController stop];
}
}
-(void)pause
{
if (_moviePlayerController)
{
[_moviePlayerController pause];
}
}
// use this to get the view form the moviePlayerController......
-(UIView*)videoView
{
UIView *view = nil;
if (_moviePlayerController)
{
view = _moviePlayerController.view;
}
return view;
}
@end
然后在视图控制器中我有:
@implementation VideoViewController
// lazy initialiser
- (StandardVideoPlayer*) videoPlayer
{
if (!_videoPlayer)
{
_videoPlayer = [[StandardVideoPlayer alloc] initWithParentView:self.view andAutoPlay:NO andFullScreen:NO andAnimated:YES];
}
return _videoPlayer;
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.initialised = NO;
self.wasPDFScreen = NO;
self.isFullScreen = NO;
[self.spinner stopAnimating];
self.spinner.hidesWhenStopped = YES;
[GlobalStore sharedInstance].videoViewController = self;
}
-(void) willEnterFullScreen
{
self.isFullScreen = YES;
}
-(void)loadStateChanged
{
MPMovieLoadState state = [self.videoPlayer.moviePlayerController loadState];
if (state & MPMovieLoadStatePlayable)
{
[self.spinner stopAnimating];
}
}
-(void) customInit
{
if (!self.initialised)
{
[self.view setBackgroundColor:[UIColor blackColor]];
[self.videoPlayer.videoView setBackgroundColor:[UIColor blackColor]];
// set a border
CGFloat borderWidth = 1.0f;
self.videoPlayer.videoView.frame = CGRectInset(self.videoPlayer.videoView.frame, -borderWidth, -borderWidth);
self.videoPlayer.videoView.layer.borderColor = BORDER_COLOUR;
self.videoPlayer.videoView.layer.borderWidth = borderWidth;
self.view.frame = CGRectInset(self.view.frame, -borderWidth, -borderWidth);
self.view.layer.borderColor = BORDER_COLOUR;
self.view.layer.borderWidth = borderWidth;
// set this to be able to exit the video player properly
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterFullScreen)
name:MPMoviePlayerWillEnterFullscreenNotification
object:_videoPlayer.moviePlayerController];
// set this to be able to start/stop the spinner
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loadStateChanged)
name:MPMoviePlayerLoadStateDidChangeNotification
object:_videoPlayer.moviePlayerController];
// make sure the views are always at the front
[self.view bringSubviewToFront:self.videoPlayer.videoView];
[self.view bringSubviewToFront:self.spinner];
self.initialised = YES;
}
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self customInit];
}
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.spinner stopAnimating];
if (_videoPlayer && self.wasPDFScreen)
{
NSTimeInterval time = [self.videoPlayer.moviePlayerController currentPlaybackTime];
if (time != 0)
{
// So to ge the video to play properly, i have to uncommment the lines below, but this loses the cache and isn't quite as smooth as i would like.
//[self stopVideo];
//self.videoPlayer.moviePlayerController.currentPlaybackTime = time;
//[self.videoPlayer.moviePlayerController prepareToPlay];
//[self.videoPlayer.moviePlayerController play];
//[self.videoPlayer.moviePlayerController pause];
}
}
self.isFullScreen = NO;
self.wasPDFScreen = NO;
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
// only stop the video when we mean to - not when we go into full screen mode
if (!self.isFullScreen && !self.wasPDFScreen)
[self stopVideo];
}
- (void)playVideo
{
[self.spinner startAnimating];
[self.videoPlayer loadFromURL:@"someurl"];
// and play!!
[self.videoPlayer play];
}
-(void)stopVideo
{
[self.videoPlayer stop];
}
@end
答案 0 :(得分:0)
我最终实现了AV套件视图控制器。完美的工作。