我的应用只是肖像,但我想让用户在通过UIWebview观看全屏视频时旋转到横向。我做了一些研究,发现我应该将我的班级添加为这些通知的观察者:
UIMoviePlayerControllerDidEnterFullscreenNotification UIMoviePlayerControllerWillExitFullscreenNotification
我像这样的观察者添加和删除类:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidEnterFullScreen:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullScreen:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
}
- (void)moviePlayerDidEnterFullScreen:(NSNotification *)notification
{
self.videoPlayingFullScreen = YES;
}
- (void)moviePlayerWillExitFullScreen:(NSNotification *)notification
{
self.videoPlayingFullScreen = NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.videoPlayingFullScreen)
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
我的问题是:我从未收到过“UIMoviePlayerControllerWillExitFullscreenNotification”。我无法使用UIMoviePlayerControllerDidExitFullscreenNotification,因为如果用户完成了横向观看全屏视频并按下“完成”,则前一个视图控制器也应以横向方式显示为横向。
还有另一种方法可以检测用户“确实”进入全屏并且“将”退出全屏吗?或者有什么我想念的东西?
修改 我的应用仅适用于iOS 7.
答案 0 :(得分:3)
您未获得UIMoviePlayerControllerWillExitFullscreenNotification
回调的原因是因为您在viewWillDisappear:
上以观察者身份移除了自己
答案 1 :(得分:0)
由于这些回调没有记录,我使用Javascript事件(如H2CO3建议here)来确定视频何时开始,结束或暂停。
顺便说一下,我正在使用YouTube播放器。
首先,我设置UIWebview并将我的ViewController设置为委托。
接下来,我将HTML文件加载到UIWebview中。
<强>的index.html 强>
<html>
<body bgcolor=#8C1717 style="margin:0px;">
<div id="ytplayer"></div>
<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady()
{
player = new YT.Player('ytplayer',
{
height: 'videoHeight',
width: 'videoWidth',
videoId: 'videoID',
playerVars: { 'showinfo':0, 'rel':0 },
events: { 'onStateChange': onPlayerStateChange }
});
}
function playerDidBeginPlaying()
{
document.location.href = "fake://video-began";
}
function playerDidEndPlaying()
{
document.location.href = "fake://video-ended";
}
var done = false;
function onPlayerStateChange(event)
{
if (event.data == YT.PlayerState.PLAYING && !done)
{
done = true;
playerDidBeginPlaying();
}
else if (event.data == YT.PlayerState.ENDED)
{
playerDidEndPlaying();
}
else if (event.data == YT.PlayerState.PAUSED)
{
playerDidEndPlaying();
}
}
</script>
</body>
</html>
在ViewController内部
NSError *error = NULL;
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error)
{
#ifdef DEBUG
NSLog(@"[YouTube Webview] Error: %@", [error description]);
#endif
}
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
然后,我实施了方法webView:shouldStartLoadWithRequest:navigationType:
,以便在事件发生时得到通知。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:@"fake://video-began"])
{
self.videoPlayingFullScreen = YES;
return NO;
}
else if ([[[request URL] absoluteString] hasPrefix:@"fake://video-ended"])
{
self.videoPlayingFullScreen = NO;
return NO;
}
return YES;
}