实现iOS全局禁止横屏,但kxmovie全屏横屏播放视频

参考我转载的文章 iOS 全局禁止横屏,但UIWebView 全屏横屏播放视频的解决办法

我本身项目时用FFMpeg + kxmovie 来播放视频

实现为如下:

1,AppDelegate 类添加参数,控制旋屏

//  AppDelegate.h

#import


@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, assign) BOOL allowRotation;/***  是否允许横屏的标记 */

@end


//  AppDelegate.m

实现协议的函数

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

//去限制只播放才横屏,其他节目都是竖屏
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}


2,KxMovieViewController 类添加进入全屏和退出全屏的函数,在初始化和返回的事件中调用

- (void)begainFullScreen {//进入全屏
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
}

- (void)endFullScreen {//退出全屏
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    
    //强制归正:
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val =UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

在初始化事件中调用

- (id) initWithContentPath: (NSString *) path parameters: (NSDictionary *) parameters {
    NSAssert(path.length > 0, @"empty path");
    
    [self begainFullScreen];//进入全屏

    ....

    ....

    ....

}

在返回事件中调用

- (void) doneDidTouch: (id) sender
{
    if (self.presentingViewController || !self.navigationController)
        [self dismissViewControllerAnimated:YES completion:nil];
    else
        [self.navigationController popViewControllerAnimated:YES];
    [self endFullScreen]; //退出全屏,而且将屏幕强制为竖屏,这句只能放在返回之后,之前会导致crash,因为旋屏设置 冲突
}


结果大致可以接受,但不够理想,因为kxmovie返回后可以看到原界面从横屏转为竖屏的动画,姑且这样吧。如果看官有好的方法,还请不吝赐教!!!

你可能感兴趣的:(iOS零碎)