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

发布于 2024-12-15 15:38:30 字数 1368 浏览 3 评论 0原文

编辑:这些视频是正在使用的应用程序的视频:即它们是设计为以纵向模式播放的教学视频。

我使用了这种方法 (注意 iPhoneDevSDK.com 目前已被 Google 标记为包含恶意软件,因此请小心。我已上传了 相关帖子的屏幕截图) 打开&播放 YouTube 视频(在幕后使用 UIWebView)。

我想要展示的视频是以纵向录制的(以 320x480 上传),旨在在纵向模式下很好地填充 iPhone 屏幕。然而,YouTube 视频以强制横向模式启动,只能通过将手机移动到横向位置然后再返回来将其转换为纵向(有时需要完成几次)。

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

注意,我已经在生成 UIWebView 的 UIWebView 委托上设置了 (interfaceOrientation == UIInterfaceOrientationPortrait)。

编辑:我什至尝试对 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
  • 第一张图片:默认情况下以横向方式播放视频
  • 第二张图片:纵向模式旋转了几次

默认为横向 在纵向模式下旋转几次

edit: The videos are of the app being used: i.e. they're instructional videos designed to be played in portrait mode.

I used this method (NB iPhoneDevSDK.com is currently marked by Google as containing malware, so be careful. I've uploaded a screengrab of the relevant post) to open & play a YouTube video (using a UIWebView behind the scenes).

The video I want to show was recorded in portrait (uploaded in 320x480) and was intended to fill the iPhone screen nicely when in portrait mode. However, the YouTube video starts in a forced landscape mode, and can only be put into portrait by moving the phone to a landscape position and then back again (sometimes needs to be done a few times).

What I'm wondering is: is there a way of forcing the UIWebView to open a video player that defaults to portrait mode?

NB I've set (interfaceOrientation == UIInterfaceOrientationPortrait) on the UIWebView delegate that's spawning the UIWebView.

Edit: I even tried subclassing UIWebView on the off-chance that this would get it to work:

@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
  • First image: video playing in Landscape by default
  • Second image: Portrait mode once rotated a few times

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2024-12-22 15:38:30

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

NSString *embedHTML = @"<iframe title=\"YouTube video player\" width=\"320\" height=\"460\" scrolling=\"no\" src=\"http://www.youtube.com/embed/%@?modestbranding=1&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]; 

I've been facing same problem recently and used this code.
This may not be the right answer to your question but it plays YouTube video in portrait mode.

NSString *embedHTML = @"<iframe title=\"YouTube video player\" width=\"320\" height=\"460\" scrolling=\"no\" src=\"http://www.youtube.com/embed/%@?modestbranding=1&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]; 
痴情换悲伤 2024-12-22 15:38:30

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

顺便说一句,

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

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

一个有用的问题,其中有很多有用的提示: iPhone SDK:方向(横向和纵向视图)

For whichever view controller that owns the UIWebView, you could specify that you only want to display everything from that view controller in portrait mode.

Something like, say:

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

and

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

B.T.W., there's a helpful question with lots of helpful hints over at: iPhone SDK: Orientation (Landscape and Portrait views)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文