我正在開發需要播放YouTube視頻的iPhone應用程序。以橫向方式在UIWebView中播放YouTube視頻,而ViewController處於縱向方向
我有一個ViewController在肖像取向只有一個UIWebView加載YouTube視頻。
我想要做的是該視頻可以橫向播放。
這聽起來很簡單,但我不知道該怎麼做。有人可以幫我嗎?
謝謝!
我正在開發需要播放YouTube視頻的iPhone應用程序。以橫向方式在UIWebView中播放YouTube視頻,而ViewController處於縱向方向
我有一個ViewController在肖像取向只有一個UIWebView加載YouTube視頻。
我想要做的是該視頻可以橫向播放。
這聽起來很簡單,但我不知道該怎麼做。有人可以幫我嗎?
謝謝!
目前沒有辦法以編程方式開始播放YouTube視頻。這就是說,你可以在ViewController的willRotateToInterfaceOrientation:duration中創建一個UIWebview:檢查水平方向,並用顯示視頻縮略圖的代碼顯示webview。但用戶仍然必須激活視圖。 要刪除視頻,請使用相同的方法進行,但要使用縱向方向並移除網頁視圖。
我找到了答案。
1)I將此代碼添加到我的viewController.m
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
2)在界面生成器我建立我在景觀接口,具有旋轉90°的每個單獨的元件。
當我建立並運行時,當它實際上是風景時,視圖似乎處於縱向模式。因此,嵌入在UIWebView中的YouTube視頻將在橫向中播放。
這發生在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
}