我使用第三方套件XCDYouTubeKit,就可以透過內建的MPMoviePlayerViewController來播放影片,那麼想要在特定的畫面橫向怎麼實現?因為整個App專案設定只有Portrait,也就是直向,只有在播放影片時要變成可以Landscape,就是橫向。
先前寫了篇文章:在WebView播放Youtube影片旋轉,確實可以做到。不過iOS 9之後又有稍微變化,只好改寫它囉~
只要在AppDelegate.m實作如下:
/**
Theme: Display Video for Landscape
IDE: Xcode 9
Language: Objective C
Date: 106/12/28
Author: HappyMan
Blog: https://cg2010studio.com/
*/
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[window.rootViewController presentedViewController]
isKindOfClass:[MPMoviePlayerViewController class]] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}else {
if ([[window.rootViewController presentedViewController]
isKindOfClass:[UINavigationController class]]) {
// look for it inside UINavigationController
UINavigationController *nc = (UINavigationController *)[window.rootViewController presentedViewController];
// is at the top?
if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
// or it’s presented from the top?
} else if ([[nc.topViewController presentedViewController]
isKindOfClass:[MPMoviePlayerViewController class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
}
return UIInterfaceOrientationMaskPortrait;
}
MPMoviePlayerViewController在iOS 9後已被棄用,目前我用Xcode 9.2會顯示黃色驚嘆號,看來未來有必要再次改寫!
Apple就是這樣,會跟著時代潮流來調整SDK,不過我們只要喜歡學習新資訊技術,就不用擔心會被時間沖淡價值。
參考:iOS9 supportedInterfaceOrientationsForWindow stops getting called。
Comments on: "[iOS] 播放影片時特定畫面橫向" (1)
[…] 在播放影片時特定畫面橫向文章提到播放影片時要橫向,那麼在播放完影片後,想要回復到直向可以怎麼做? […]
讚讚