1. 程式人生 > >iOS 視訊播放橫豎屏的轉化

iOS 視訊播放橫豎屏的轉化

我們在做視訊播放開發的時候,常常會用到橫屏的播放,今天就記錄下我做專案時候做橫屏的方法,主要是使用註冊通知,然後識別手機是否旋轉方向去旋轉橫屏。
思路:
獲取到手機已經旋轉的通知以後,先改變播放器的transform進行旋轉,然後改變frame 去改變播放器的大小。
核心程式碼如下:

/** 非全屏狀態下播放器 superview */
@property (nonatomic, strong) UIView *originalSuperview;
/** 非全屏狀態下播放器 frame 作為中間值用來記錄原來的frame */
@property (nonatomic, assign) CGRect originalRect;
/** 是否是全屏狀態*/
@property (nonatomic) BOOL isfullScreen;


/* 監聽螢幕旋轉   註冊通知*/
- (void)screenRotationNotification{
    //監聽螢幕旋轉
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)setIsfullScreen:(BOOL)isfullScreen{
    _isfullScreen = isfullScreen;
//    self.controlPlayer.IsfullScreen = _isfullScreen;
}
/** 螢幕翻轉監聽事件 */
- (void)orientationChanged:(NSNotification *)notify
{
    [self orientationAspect];
}
/** 根據螢幕旋轉方向改變當前視訊螢幕狀態 */
- (void)orientationAspect
{
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationLandscapeLeft){
        if (!_isfullScreen){
            [self videoZoomInWithDirection:UIInterfaceOrientationLandscapeRight];
        }
    }
    else if (orientation == UIDeviceOrientationLandscapeRight){
        if (!_isfullScreen){
            [self videoZoomInWithDirection:UIInterfaceOrientationLandscapeLeft];
        }
    }
    else if(orientation == UIDeviceOrientationPortrait){
        if (_isfullScreen){
            [self videoZoomOut];
        }
    }
}
/**
 視訊放大全螢幕
 @param orientation 旋轉方向
 */
- (void)videoZoomInWithDirection:(UIInterfaceOrientation)orientation
{
    _originalSuperview = self.superview;
    _originalRect = self.frame;

/* 下面的方法是把播放器新增到keyWindow上  這樣便於轉化    但是有個問題就是會覆蓋播放器上面的控制器檢視,橫屏展示時會變為第一檢視,甚至會蓋住狀態列

我們可以根據isfullScreen 去寫個代理 ,去控制器介面改變播放器的frame來解決*/
//    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
//    [keyWindow addSubview:self];
    
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    //    [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO];
    [UIView animateWithDuration:duration animations:^{
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(screenRotationChange:)]) {
            [self.delegate screenRotationChange:YES];
        }
        if (orientation == UIInterfaceOrientationLandscapeLeft){
            self.transform = CGAffineTransformMakeRotation(-M_PI/2);
        }else if (orientation == UIInterfaceOrientationLandscapeRight) {
            self.transform = CGAffineTransformMakeRotation(M_PI/2);
        }
    }completion:^(BOOL finished) {}];
    
    self.frame = [UIScreen mainScreen].bounds;
    [self setNeedsLayout];
    [self layoutIfNeeded];
    self.isfullScreen = YES;
    
}

// 視訊退出全螢幕
- (void)videoZoomOut
{
    //退出全屏時強制取消隱藏狀態列
    //    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    //    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    [UIView animateWithDuration:duration animations:^{
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(screenRotationChange:)]) {
            [self.delegate screenRotationChange:NO];
        }
        self.transform = CGAffineTransformMakeRotation(0);
    }completion:^(BOOL finished) {
        
    }];
    self.frame = _originalRect;
    [_originalSuperview addSubview:self];
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    self.isfullScreen = NO;
}