xcode - 仅在播放视频时使用横向模式。代码。 IOS 7

标签 xcode video ios7 youtube landscape

我使用 youtube-ios-player-helper:https://github.com/youtube/youtube-ios-player-helperhttps://developers.google.com/youtube/v3/guides/ios_youtube_helper

我一切正常。视频播放没问题! 但! 在项目设置中,我关闭了横向模式。因此,视频只能以纵向模式播放。在 iOS 7 中播放视频时如何打开横向模式?

最佳答案

我的解决方案:

它适用于 iOS 7 和 iOS 8

为了播放来自 YouTube 的视频,我使用了 XCDYouTubeKit ( https://github.com/0xced/XCDYouTubeKit )

AppDelegate.h:

@property (nonatomic) BOOL screenIsPortraitOnly;

AppDelegate.m:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    if (!self.screenIsPortraitOnly) {
        return UIInterfaceOrientationMaskPortrait;
    }
    else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

MYTableViewController.m:

#import "XCDYouTubeVideoPlayerViewController.h"
#import "XCDYouTubeKit.h"
#import "AppDelegate.h"

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:myIdYoutube];
    [[NSNotificationCenter defaultCenter] removeObserver:videoPlayerViewController  name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerViewController.moviePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerViewController.moviePlayer];
    videoPlayerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:videoPlayerViewController animated:YES completion:nil];
}

-(void)videoFinished
{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = false;
    [self dismissViewControllerAnimated:YES completion:NULL];
}

XCDYouTubeVideoPlayerViewController.m

#import "AppDelegate.h"

- (instancetype) initWithVideoIdentifier:(NSString *)videoIdentifier
{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = true;
    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8)
        self = [super initWithContentURL:nil];
    else
        self = [super init];

    if (!self)
        return nil;

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

    if (videoIdentifier)
        self.videoIdentifier = videoIdentifier;

    return self;
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = false;

    if (![self isBeingDismissed])
        return;

    [self.videoOperation cancel];
}

关于xcode - 仅在播放视频时使用横向模式。代码。 IOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25346587/

相关文章:

ios - UINavigationController 在 iOS 7 上像模式一样推送

ios - 使用 Google Firebase 通知构建错误

ios - 如何通过segue传递Section和Row指定的tableview单元格内容?

video - ffmpeg的视频文件的持续时间是否准确?

html - 一次仅播放一个视频 HTML5

ios - 如何使纵向模式下的视频适合方形单元格?

ios - MFMailComposeViewController 忽略 iOS 7 中的一些 UIAppearance 协议(protocol)

ios - 拖动创建一条线

ios - Xcode 检测 iOS 版本并相应地显示 Storyboard

ios - 尝试从 Safari 启动应用程序时不断收到 "Open this page in YourAppName?"的警报 View 消息