【发布时间】:2011-09-08 04:34:16
【问题描述】:
我正在开发一个需要播放 YouTube 视频的 iPhone 应用程序。
我有一个纵向方向的 ViewController,只有一个加载 YouTube 视频的 UIWebView。
我想做的是视频可以横向播放。
这听起来很简单,但我不知道该怎么做。有人可以帮帮我吗?
谢谢!
【问题讨论】:
标签: ios xcode youtube uiwebview landscape
我正在开发一个需要播放 YouTube 视频的 iPhone 应用程序。
我有一个纵向方向的 ViewController,只有一个加载 YouTube 视频的 UIWebView。
我想做的是视频可以横向播放。
这听起来很简单,但我不知道该怎么做。有人可以帮帮我吗?
谢谢!
【问题讨论】:
标签: ios xcode youtube uiwebview landscape
这在 AppDelegate 中。将为视频播放器启用水平模式
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let presentedViewController = window?.rootViewController?.presentedViewController {
let className = String(describing: type(of: presentedViewController))
if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className)
{
return UIInterfaceOrientationMask.allButUpsideDown
}
}
return UIInterfaceOrientationMask.portrait
}
【讨论】:
目前无法以编程方式开始播放 YouTube 视频。 也就是说,您可以在 ViewController 的 willRotateToInterfaceOrientation:duration: 中创建一个 UIWebview:检查水平方向并使用显示视频缩略图的代码显示 webview。但是用户仍然必须激活视图。 要删除视频,您可以使用相同的方法执行此操作,但要针对纵向并删除 webview。
【讨论】:
我找到了答案。
1) 我将此代码添加到我的 viewController.m
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
2) 在界面构建器中,我在 Landscape 中构建界面,每个元素都旋转了 90°。
当我构建并运行时,视图似乎处于纵向模式,而实际上它是横向模式。因此,嵌入在 UIWebView 中的 YouTube 视频以横向播放。
【讨论】: