iPhone/iPad 启动视频的实现
原理
一些应用,在启动的时候加载了视频,感觉非常不错。实质上很简单:就是在 window的rootViewController上实现视频播放功能。下面的代码例子是实现播放在线视频功能,点击可退出。
1. AppDelegate
// OpenVideoController.h#import <UIKit/UIKit.h>#import <MediaPlayer/MediaPlayer.h>@protocol OpenVideoControllerDelegate <NSObject>-(void)openVideoControllerDidStop;@end@interface OpenVideoController : MPMoviePlayerViewController@property(assign,atomic) BOOL isCallbacked;@property(assign)id<OpenVideoControllerDelegate> delegate;-(id)initWithDefault;@end// OpenVideoController.m#import "OpenVideoController.h"#import "SVProgressHUD.h"@interface OpenVideoController (PrivateOpenVideoController)-(void)popOpenVideo;@end@implementation OpenVideoController@synthesize delegate;@synthesize isCallbacked;-(id)initWithDefault{ NSURL *contentURL = [NSURL URLWithString: @"http://url-to-video/opening.mp4"];//online //NSURL *contentURL = [[NSBundle mainBundle] URLForResource:@"opening" withExtension:@"mp4"];//local self = [super initWithContentURL:contentURL]; if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(playbackFinished:) name: MPMoviePlayerPlaybackDidFinishNotification object :nil]; isCallbacked = FALSE; } return self;}- (void)viewDidLoad{ [super viewDidLoad]; self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill; self.moviePlayer.controlStyle = MPMovieControlStyleNone; [self.moviePlayer prepareToPlay]; [self.moviePlayer play]; UIView *tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 1024)]; tapView.backgroundColor = [UIColor clearColor]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap:)]; [tapView addGestureRecognizer:tap]; [self.view addSubview:tapView];}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [SVProgressHUD showWithStatus:@"视频缓冲中...点击跳过."];}-(void)viewTap:(id)sender{ //NSLog(@"-----viewTap: %d",self.moviePlayer.playbackState); [self popOpenVideo]; }-(void)playbackStateDidChange:(NSNotification *)sender{ NSLog(@"-----playbackStateDidChange: %d",self.moviePlayer.playbackState); if (self.moviePlayer.playbackState == MPMoviePlaybackStateStopped ) { [self popOpenVideo]; }}- (void)playbackFinished:(NSNotification*)notification{ NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; switch ([reason intValue]) { case MPMovieFinishReasonPlaybackEnded: NSLog(@"-----Playback Ended"); break; case MPMovieFinishReasonPlaybackError: NSLog(@"-----Playback Error"); break; case MPMovieFinishReasonUserExited: NSLog(@"-----User Exited"); break; default: break; } NSLog(@"-----playbackFinished: %d",[reason intValue]); [self popOpenVideo];}-(void)loadStateDidChange:(NSNotification *)sender{ //NSLog(@"-----loadStateDidChange: %d",self.moviePlayer.loadState); if(self.moviePlayer.loadState == MPMovieLoadStatePlayable){ [SVProgressHUD dismiss]; }}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return UIInterfaceOrientationIsLandscape(interfaceOrientation); //return YES;}-(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; [SVProgressHUD dismiss]; //NSLog(@"--------open video controller delloc");}-(void)popOpenVideo{ @synchronized(self){ if (!isCallbacked) { if (delegate && [delegate respondsToSelector:@selector(openVideoControllerDidStop)]) { isCallbacked = TRUE; [delegate openVideoControllerDidStop]; } } }}@end
4. MainViewController
MainViewController 就是普通的 ViewController 了,主要是替换 window 的 rootController
1. 由于加载的是在线视频,所以,使用了类库:SVProgressHUD
2. 基于上面原因,若是本地视频的话,则可去了 SVProgressHUD 相关的代码。
3. 总体来说,代码还是相当简单的。
THE END.