自动播放从HTML5< video>播放的视频在UIWebView中 [英] Autorotating video played from HTML5 <video> in UIWebView

查看:99
本文介绍了自动播放从HTML5< video>播放的视频在UIWebView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序由于某些原因,仅支持纵向方向。

但是,在某些情况下,我需要显示来自UIWebView的视频(带视频

My app, for some reasons, only supports portrait orientation.
However, in some cases I need to display videos from a UIWebView (with video tag) and it would be nice if the user can view it either in portrait or landscape.

控制器配置如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

结果:视频仅以纵向播放模式(好的,非常期待)。

Result: the video is played only in portrait mode (ok, quite expected).

我试过:

- 设置此项以支持用户启动视频时的所有方向
b $ b - 当视频停止时,返回仅限肖像

I tried:
- setting this to support all orientations when the user starts a video
- when the video stops, back to "portrait only"

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    // autorotationEnabled is toggled when a video is played / stopped
    return autorotationEnabled ? YES : UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

结果:横向模式可用(太棒了! )但是如果用户在横向播放时点击完成,则一旦玩家被解雇(不太好),之前的视图将以横向模式显示。

Result: landscape mode is available (great!) but if the user taps 'Done' while playing in landscape, the previous view will appear in landscape mode once the player is dismissed (not so great).

是否有人想知道如何在玩家被解雇时阻止控制器以横向模式显示?

(使用UIDevice的私有方法setInterfaceOrientation不是一个选项)

Does anybody have an idea on how to prevent the controller from being displayed in landscape mode when the player is dismissed?
(using the private method setInterfaceOrientation of UIDevice is not an option)

推荐答案

我做过非常相似的事情。当你弹出控制器时,你必须修改UIView堆栈以强制app调用shouldAutorotateToInterfaceOrientation。

I've have done something very similar. You have to modify the UIView stack to force the app to call shouldAutorotateToInterfaceOrientation when you pop the controller.

在你的WebViewController中设置允许自动旋转:

In your WebViewController set to allow the autorotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}

在父控制器中禁止自动旋转:

In the parent controller disallow autorotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

创建并分配UINavigationControllerDelegate。

在委托中,当控制器弹出或推动时暂时修改UIView堆栈:

Create and assign a UINavigationControllerDelegate.
In the delegate, temporarily modify the UIView stack when the controller pops or pushes:

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

// Force portrait by changing the view stack to force autorotation!
if ([UIDevice currentDevice].orientation != UIInterfaceOrientationPortrait) {
    if (![viewController isKindOfClass:[MovieWebViewController class]]) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UIView *view = [window.subviews objectAtIndex:0];
        [view removeFromSuperview];
        [window insertSubview:view atIndex:0];
    }
}

这是一种肮脏的黑客,但它确实有效。

It's kind of a dirty hack, but it works.

这篇关于自动播放从HTML5< video>播放的视频在UIWebView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆