实现思路
主要思路就是用一个控制器来作为播放视频的载体,然后在让这个控制器作为根视图,视频播放完成之后那就该干嘛干嘛了。
话不多说了,下面就放代码好了
先新建一个控制器AnimationViewController
在控制器中新建一个属性moviePlayer
,记得要先引入系统库<MediaPlayer/MediaPlayer.h>
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;设置
moviePlayer
我是在懒加载中直接设置的-(MPMoviePlayerController *)moviePlayer{if (!_moviePlayer) {_moviePlayer = [[MPMoviePlayerController alloc]init];[_moviePlayer.view setFrame:self.view.bounds];//设置自动播放[_moviePlayer setShouldAutoplay:NO];//设置源类型 因为新特性一般都是播放本地的小视频 所以设置源类型为file_moviePlayer.movieSourceType = MPMovieSourceTypeFile;//取消控制视图 如:播放暂停等_moviePlayer.controlStyle = MPMovieControlStyleNone;[self.view addSubview:_moviePlayer.view];//监听播放完成[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playFinsihed) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];}return _moviePlayer;}然后在
.h
中公开一个moviePath
视频的路径,还有一个结束播放的blockplayFinished
等下需要。AnimationViewController
中也算差不多了,毕竟也没什么东西,接下来我们去AppDelegate
中声明一个AnimationViewController
属性- (AnimationViewController *)animationViewController{if (!_animationViewController) {_animationViewController = [[AnimationViewController alloc]init];//设置本地视频路径_animationViewController.moviePath = [[NSBundle mainBundle] pathForResource:@"V" ofType:@"mp4"];_animationViewController.playFinished = ^{UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];[UIApplication sharedApplication].keyWindow.rootViewController = rootNav;};}return _animationViewController;}然后在
AppDelegate
的启动方法把这个控制器设为根视图- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];self.window.rootViewController = self.animationViewController;[self.window makeKeyAndVisible];return YES;}总结