播放视频时仅支持横屏模式。Xcode。iOS 7。

3

你已经解决了这个问题吗? :) - directory
1个回答

0

我的解决方案:

适用于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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接