在UIWebView中播放YouTube视频时强制纵向模式

时间:2011-11-15 14:28:45

标签: objective-c uiwebview youtube portrait

编辑:视频属于正在使用的应用:即,它们是专为在纵向模式下播放的教学视频。

我使用了this method (NB iPhoneDevSDK.com目前被Google标记为包含恶意软件,所以要小心。我已经上传了screengrab of the relevant post来打开&播放YouTube视频(在幕后使用UIWebView)。

我要显示的视频是以肖像方式录制的(以320x480上传),用于在纵向模式下很好地填充iPhone屏幕。但是,YouTube视频以强制横向模式启动,只能通过将手机移至横向位置然后再返回(有时需要完成几次)才能进入人像。

我想知道的是:有没有办法迫使UIWebView打开默认为纵向模式的视频播放器?

注意我在UIWebView委托上设置了(interfaceOrientation == UIInterfaceOrientationPortrait),该委托产生了UIWebView。

编辑:我甚至尝试将UIWebView子类化,但这很可能会让它工作:

@interface AltWebView : UIWebView
@end

@implementation AltWebView

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

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

@end
  • 第一张图片:默认情况下在横向播放视频
  • 第二张图像:纵向模式一次旋转几次

In Landscape by default In Portrait mode once rotated a few times

2 个答案:

答案 0 :(得分:4)

我最近遇到了同样的问题并使用了这段代码。 这可能不是您问题的正确答案,但它会以纵向模式播放YouTube视频。

NSString *embedHTML = @"<iframe title=\"YouTube video player\" width=\"320\" height=\"460\" scrolling=\"no\" src=\"http://www.youtube.com/embed/%@?modestbranding=1&amp;rel=0;autoplay=1;showinfo=0;loop=1;autohide=1\" frameborder=\"0\" allowfullscreen allowTransparency=\"true\"></iframe>"; 

NSString *htmlStr = [NSString stringWithFormat:embedHTML, videoID];  // videoID is like EgXdUs9HsrQ...  

[webView loadHTMLString:htmlStr baseURL:nil]; 

答案 1 :(得分:1)

对于拥有UIWebView的任何视图控制器,您可以指定只想以纵向模式显示该视图控制器中的所有内容。

有点像,说:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

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

B.T.W。,这是一个有用的问题,有很多有用的提示:iPhone SDK: Orientation (Landscape and Portrait views)

相关问题