用mp4(影片),作為APP開頭動畫

用mp4(影片),作為APP開頭動畫

加入Framework, MediaPlayer.framework
add_framework 
引入header, #import <MediaPlayer/MediaPlayer.h>

@interface RootViewController ()
{  
}
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
 
@implementation RootViewController
@synthesize moviePlayer;
 
-(void)playIntro{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"appIntro" ofType:@"mp4"]];
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    //註冊通知中心,當影片播放完畢,進行回呼方法(playbackStateDidFinish)
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateDidFinish)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayer ];
    //播放器參數設定
    self.moviePlayer.controlStyle = MPMovieControlStyleNone;
    self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
    self.moviePlayer.repeatMode = MPMovieRepeatModeNone;
    self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin
   |UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
    //設定播放器大小
    [self.moviePlayer.view setFrame:self.view.bounds];
    [self.view addSubview:self.moviePlayer.view];
    //播放
    [self.moviePlayer play];
}
 
- (void)playbackStateDidFinish{
    //註銷通知中心
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayer];
    //停止播放
    [self.moviePlayer stop];
    //移除播放器
    [self.moviePlayer.view removeFromSuperview];
}