关于视频播放

来源:互联网 发布:tcp和udp端口号区别 编辑:程序博客网 时间:2024/05/20 23:58

视频退出后台后再次进到前台视频暂停

需要监听这两个通知,然后对视频播放做相应处理

  • // app退到后台
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationWillResignActiveNotification object:nil];

  • // app进入前台
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterPlayground) name:UIApplicationDidBecomeActiveNotification object:nil];

旋转屏幕改变视频的View

监听方法:

  • // 监测设备方向
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(onDeviceOrientationChange)
    name:UIDeviceOrientationDidChangeNotification
    object:nil];
    实现:
#pragma mark - 屏幕方向发生变化会调用这里- (void)onDeviceOrientationChange {    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;    if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown ) { return; }    switch (interfaceOrientation) {        case UIInterfaceOrientationPortrait:{            [self verticalScreen];        }            break;        case UIInterfaceOrientationLandscapeLeft:{            [self crossScreen:UIInterfaceOrientationLandscapeLeft];        }            break;        case UIInterfaceOrientationLandscapeRight:{            [self crossScreen:UIInterfaceOrientationLandscapeRight];        }            break;        default:            break;    }}#pragma mark - 横屏- (void)crossScreen:(UIInterfaceOrientation)orien{    // 隐藏导航栏    [self.viewController.navigationController setNavigationBarHidden:YES animated:NO];    // 获取到当前状态条的方向    UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;    // 判断如果当前方向和要旋转的方向一致,那么不做任何操作    if (currentOrientation == orien) { return; }    UIWindow *win = [[UIApplication sharedApplication] windows].lastObject;    // 根据要旋转的方向,使用Masonry重新修改限制    if (orien != UIInterfaceOrientationPortrait) {//        // 这个地方加判断是为了从全屏的一侧,直接到全屏的另一侧不用修改限制,否则会出错;        if (currentOrientation == UIInterfaceOrientationPortrait) {            self.frame = CGRectMake(0, 0, win.bounds.size.height, win.bounds.size.width-20);            [win addSubview:self];        }    }    if (orien == UIInterfaceOrientationLandscapeLeft){        self.center = CGPointMake(win.center.x+10, win.center.y);    }else{        self.center = CGPointMake(win.center.x-10, win.center.y);    }    [[UIApplication sharedApplication] setStatusBarOrientation:orien animated:NO];    _flag = false;    // 旋转    [self rotateView];    [self setSubViewsFrame];  // 改变位置}#pragma mark - 旋转view- (void)rotateView{    // 获取旋转状态条需要的时间:    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:0.3];    // 更改了状态条的方向,但是设备方向UIInterfaceOrientation还是正方向的,这就要设置给你播放视频的视图的方向设置旋转    // 给你的播放视频的view视图设置旋转    self.transform = CGAffineTransformIdentity;    self.transform = [self getTransformRotationAngle];    // 开始旋转    [UIView commitAnimations];}#pragma mark - 设置控件大小- (void)setSubViewsFrame{    width = self.bounds.size.width;    height= self.bounds.size.height;    self.playBtn.frame = CGRectMake(10, height-bottomHeight, 20, 20);    self.startLab.frame = CGRectMake(CGRectGetMaxX(self.playBtn.frame)+10, height-bottomHeight, 40, 20);    self.bigBtn.frame = CGRectMake(ScreenWidth-30, height-bottomHeight, 20, 20);    self.totalLab.frame = CGRectMake(CGRectGetMinX(self.bigBtn.frame)-50, height-bottomHeight, 40, 20);    self.slider.frame = CGRectMake(CGRectGetMaxX(self.startLab.frame)+5,height-bottomHeight,ScreenWidth-CGRectGetWidth(self.playBtn.frame)-CGRectGetWidth(self.bigBtn.frame)-CGRectGetWidth(self.bigBtn.frame)-CGRectGetWidth(self.startLab.frame)-70,20);    self.playLayer.frame = CGRectMake(0, 0, width, height);    self.indicatorView.center     = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);    [self addSubview:self.indicatorView];}#pragma mark - 获取变换的旋转角度- (CGAffineTransform)getTransformRotationAngle {    // 状态条的方向已经设置过,所以这个就是你想要旋转的方向    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;    // 根据要进行旋转的方向来计算旋转的角度    if (orientation == UIInterfaceOrientationPortrait) {        return CGAffineTransformIdentity;    } else if (orientation == UIInterfaceOrientationLandscapeLeft){        return CGAffineTransformMakeRotation(-M_PI_2);    } else if(orientation == UIInterfaceOrientationLandscapeRight){        return CGAffineTransformMakeRotation(M_PI_2);    }    return CGAffineTransformIdentity;}

Demo请点击

原创粉丝点击