AVPlayer和GPUImagede学习iOS开发iOS音视频大全

iOS使用AVPlayer自定义视频播放器

2017-04-15  本文已影响1981人  九剑仙

上一篇:iOS使用AVPlayer自定义音频播放器
iOS内部提供的有三种视频播放的方式,且都能播放本地、远程音频、视频文件。

  1. AVPlayer,基于Layer显示,具有高度的灵活性,可根据需要自定义UI,实现起来相对麻烦
  2. MPMoviePlayerController,自带播放控制面板,可控性低
  3. MPMoviePlayerViewController,高度封装,播放界面默认就是全屏的,如果播放功能比较简单,建议用这个。

这篇文章,我们主要讲一下第一种播放方式<AVPlayer>,毕竟后两种方式的可控性太低了,根本满足不了我们蛋疼的项目经理的要求。

LLVideoPlayer-横屏.png LLVideoPlayer-竖屏.png
蛋疼的项目经理让我们模仿优酷、模仿爱奇艺,可怜我们做的只是一款社交聊天软件,呜呼哀哉~

我写了一个简单的demo,页面相当精简,但是该实现的功能都实现了,左边调节亮度、右边调节声音等等也都实现了,有兴趣的童鞋可以下载看一下。

github地址:https://github.com/wangzhaomeng/LLVideoPlayer

延伸知识

我们的项目是这种要求,只有在播放视频的页面才支持横屏其他页面都是竖屏,因此,需要做以下处理,首先:

屏幕快照 2017-04-15 下午7.27.37.png
然后,重点中的重点,在AppDelegate中实现如下方法:
屏幕快照 2017-04-15 下午7.30.11.png
//只支持竖屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskPortrait;
}

之后在需要横屏的试图控制器中,监听当前手机的横竖屏状态,使用代码强制旋转屏幕

- (void)viewDidLoad {
    [super viewDidLoad];
    //监听横竖屏切换
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

//横竖屏切换
- (void)orientationChanged:(NSNotification *)notification {
    UIDeviceOrientation currentOrientation = [UIDevice currentDevice].orientation;
    if (currentOrientation == UIDeviceOrientationFaceUp) return;
    static CGFloat rotation;
    if (currentOrientation == UIDeviceOrientationLandscapeLeft) {
        rotation = 0.5;
    }
    else if (currentOrientation == UIDeviceOrientationLandscapeRight) {
        rotation = -0.5;
    }
    else {
        rotation = 0;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeRotation(M_PI*(rotation));
            self.view.frame = SCREEN_BOUNDS;
        }];
    });
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

播放过程中的具体细节处理,demo里面注释的很详细,不懂的地方欢迎留言。

觉得好,请给个star,谢谢!

上一篇下一篇

猜你喜欢

热点阅读