ios 程序猿FamilyiOSiOS 开发每天分享优质文章

iOS 关于启动app播放视频功能

2016-10-29  本文已影响3670人  谁遇而安

我们经常会看到我们在第一次使用app的时候,会播放一段视频,以便我们来了解app。如何实现呢,先看效果:

1、我们要在LaunchScreen.storyboard中给app一张启动图片,防止我们在启动的时候会出现空白的情况。

2、创建一个ZYLauchMovieViewController控制器,继承自AVPlayerViewController,设置我们的window的根控制器为我们要播放视频的控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

ZYLauchMovieViewController *vc = [[ZYLauchMovieViewController alloc] init];

window.rootViewController = vc;

self.window = window;

[self.window makeKeyAndVisible];

return YES;

}

注意:

3、设置四个属性,

1、播放开始之前的图片startPlayerImageView,防止出现留黑的问题,

2、播放中断的图片pausePlayerImageView,也是防止跳转的时候出现问题,

3、定时器timer,为了在进入app3秒之后显示进入应用按钮,

4、结束按钮也就是进入应用的按钮enterMainButton,为了让用户跳过视频。

/** 播放开始之前的图片 */

@property (nonatomic , strong)UIImageView *startPlayerImageView;

/** 播放中断时的图片 */

@property (nonatomic , strong)UIImageView *pausePlayerImageView;

/** 定时器 */

@property (nonatomic , strong)NSTimer *timer;

/** 结束按钮 */

@property (nonatomic , strong)UIButton *enterMainButton;

4、viewDidLoad中设置一些界面,在并且判断是不是第一次进入app,如果是第一次,设置进入主界面的按钮

// 设置界面

- (void)setupView {

self.startPlayerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lauch"]];

_startPlayerImageView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);

[self.contentOverlayView addSubview:_startPlayerImageView];

//是否是第一次进入视频

if (![self isFirstLauchApp]) {

//设置进入主界面的按钮

[self setupEnterMainButton];

}

}

//设置进入主界面的按钮

- (void)setupEnterMainButton {

self.enterMainButton = [UIButton buttonWithType:UIButtonTypeCustom];

_enterMainButton.frame = CGRectMake(24, kScreenHeight - 32 - 48, kScreenWidth - 48, 48);

_enterMainButton.layer.borderWidth =1;

_enterMainButton.layer.cornerRadius = 24;

_enterMainButton.layer.borderColor = [UIColor whiteColor].CGColor;

[_enterMainButton setTitle:@"进入应用" forState:UIControlStateNormal];

_enterMainButton.hidden = YES;

[self.view addSubview:_enterMainButton];

[_enterMainButton addTarget:self action:@selector(enterMainAction:) forControlEvents:UIControlEventTouchUpInside];

//设置定时器当视频播放到第三秒时 展示进入应用

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showEnterMainButton) userInfo:nil repeats:YES];

}

在这里我们开一个定时器,让按钮在3s之后再显示

//显示进入按钮(定时器的方法)

- (void)showEnterMainButton {

AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:self.player.currentItem.asset];

gen.appliesPreferredTrackTransform = YES;

NSError *error = nil;

CMTime actualTime;

CMTime now = self.player.currentTime;

[gen setRequestedTimeToleranceAfter:kCMTimeZero];

[gen setRequestedTimeToleranceBefore:kCMTimeZero];

[gen copyCGImageAtTime:now actualTime:&actualTime error:&error];

NSInteger currentPlayBackTime = (NSInteger)CMTimeGetSeconds(actualTime);

if (currentPlayBackTime >= 3) {

self.enterMainButton.hidden = NO;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

self.enterMainButton.alpha = 0;

[UIView animateWithDuration:0.5 animations:^{

self.enterMainButton.alpha = 1;

} completion:nil];

});

}

if (currentPlayBackTime > 5) {

//防止没有显现出来

self.enterMainButton.alpha = 1;

self.enterMainButton.hidden = NO;

[self.timer invalidate];

self.timer = nil;

}

}

//进入应用的按钮点击事件

- (void)enterMainAction:(UIButton *)btn {

//视频暂停

[self.player pause];

self.pausePlayerImageView = [[UIImageView alloc] init];

_pausePlayerImageView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);

[self.contentOverlayView addSubview:_pausePlayerImageView];

self.pausePlayerImageView.contentMode = UIViewContentModeScaleAspectFit;

//获取当前暂停时的截图,也就是设置结束时候的图片

[self getoverPlayerImage];

}

//截图的获取方法

- (void)getoverPlayerImage {

AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:self.player.currentItem.asset];

gen.appliesPreferredTrackTransform = YES;

NSError *error = nil;

CMTime actualTime;

CMTime now = self.player.currentTime;

[gen setRequestedTimeToleranceAfter:kCMTimeZero];

[gen setRequestedTimeToleranceBefore:kCMTimeZero];

CGImageRef image = [gen copyCGImageAtTime:now actualTime:&actualTime error:&error];

if (!error) {

UIImage *thumb = [[UIImage alloc] initWithCGImage:image];

self.pausePlayerImageView.image = thumb;

}

NSLog(@"%f , %f",CMTimeGetSeconds(now),CMTimeGetSeconds(actualTime));

NSLog(@"%@",error);

//视频播放结束

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[self moviePlaybackComplete];

});

}

5、在viewDidLoad设置监听,监听视频播放的开始,结束,以及程序进入活动的状态,需要判断是不是第一次进入app,是的话就重复播放视频,不是的话就播放一次

//设置监听

- (void)addNotification {

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

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

if ([self isFirstLauchApp]) {

//第二次进入app视频需要直接结束

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];//视频播放结束

}else {

//第一次进入app视频需要轮播

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackAgain) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];//视频播放结束

}

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackStart) name:AVPlayerItemTimeJumpedNotification object:nil];//播放开始

}

//实现监听的方法

//开始播放移除我们刚开始加的图片,不然无法看到视频

- (void)moviePlaybackStart {

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[self.startPlayerImageView removeFromSuperview];

self.startPlayerImageView = nil;

});

}

//视频播放完成移除图片,进入主界面

- (void)moviePlaybackComplete {

//发送推送之后就删除  否则 界面显示有问题

[[NSNotificationCenter defaultCenter] removeObserver:self

name:AVPlayerItemDidPlayToEndTimeNotification

object:nil];

[self.startPlayerImageView removeFromSuperview];

self.startPlayerImageView = nil;

[self.pausePlayerImageView removeFromSuperview];

self.pausePlayerImageView = nil;

if (self.timer){

[self.timer invalidate];

self.timer = nil;

}

//进入主界面

[self enterMain];

}

//app进入活动时播放视频

- (void)viewWillEnterForeground

{

NSLog(@"app enter foreground");

if (!self.player) {

[self prepareMovie];

}

//播放视频

[self.player play];

}

//第一次进入app的时候,再一次播放视频

- (void)moviePlaybackAgain {

self.startPlayerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lauchAgain"]];

_startPlayerImageView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);

[self.contentOverlayView addSubview:_startPlayerImageView];

[self.pausePlayerImageView removeFromSuperview];

self.pausePlayerImageView = nil;

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"opening_long_1080*1920.mp4" ofType:nil];

//初始化player

self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];

self.showsPlaybackControls = NO;

//播放视频

[self.player play];

}

6、在viewDidLoad中初始化视频

- (void)prepareMovie {

//首次运行

NSString *filePath = nil;

if (![self isFirstLauchApp]) {

//第一次安装

filePath = [[NSBundle mainBundle] pathForResource:@"opening_long_1080*1920.mp4" ofType:nil];

[self setIsFirstLauchApp:YES];

}else {

filePath = [[NSBundle mainBundle] pathForResource:@"opening_short_1080*1920.mp4" ofType:nil];

}

//初始化player

self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];

self.showsPlaybackControls = NO;

//播放视频

[self.player play];

}

7、进入主界面的方法以及判断是否第一次进入app

//进入主界面

- (void)enterMain {

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

UIViewController *main = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];

delegate.window.rootViewController = main;

[delegate.window makeKeyWindow];

}

#pragma mark -- 是否第一次进入app

- (BOOL)isFirstLauchApp {

return [[NSUserDefaults standardUserDefaults] boolForKey:kIsFirstLauchApp];

}

- (void)setIsFirstLauchApp:(BOOL)isFirstLauchApp

{

[[NSUserDefaults standardUserDefaults] setBool:isFirstLauchApp forKey:kIsFirstLauchApp];

}

大概就是这些

最后,总结一下,思路就是:

第一步:先给一张初始的图片,然后判断是不是第一次进入app,第一次就设置一个进入应用的按钮,点击截图一张放在视频上方进入主界面,然后开一个定时器,当视频播放到第3s的时候显示按钮。

第二步:监听视频的开始播放、结束播放,以及app是否进入活动状态;开始播放的时候移除第一张图片;结束播放的时候移除图片,移除定时器,进入主界面;进入活动状态时候播放视频。

第三步:初始化player,加载本地视频,播放视频

最后附上DEMO的地址需要的话可以看一下:https://github.com/zhangyqyx/startMovie

大概就这些了,希望大家能提出宝贵的意见,可以给我留言,也可以发邮件到我的邮箱:namezyqyx@163.com

谢谢大家,如果你有更好的想法或文章请告知,不胜感激。

上一篇下一篇

猜你喜欢

热点阅读