Search code examples
iosobjective-ciphonesplash-screenmpmovieplayercontroller

Video Splash Screen for ios application


I have a client that wants to have a regular picture splash screen followed by a video splash screen and then into the application itself. He got the idea from another application he saw. I recorded the intro of the app and its in the link below.

It has a picture splash screen and then a video with no controls and no user interaction with the video and upon completion dismiss of the video and into the application itself.

http://www.jportdev.com/MISC/Sample.mov

The image splash screen I have no issue doing but loading the video upon completion is where I am having some issue.

I am doing this on the side as a POC, to then implemented it in the actual app. I figure all of it will have to be done in the appDelegate... so this is what I have so far.

#import "AppDelegate.h"
#import <MediaPlayer/MediaPlayer.h>
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];

//original code generated by xcode
//self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
//self.window.rootViewController = self.viewController;
[self showIntro];
return YES;
}

-(void)showIntro{
UIImage *splashImage = [UIImage imageNamed:@"splash.jpg"];
UIImageView *splashImageView = [[UIImageView alloc] initWithImage:splashImage];
splashImageView.contentMode = UIViewContentModeScaleAspectFit;

[self.window.rootViewController.view addSubview:splashImageView];
[self.window.rootViewController.view bringSubviewToFront:splashImageView];
[UIView animateWithDuration:1.5f
                      delay:2.0f
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     splashImageView.alpha = .0f;
                     CGFloat x = -60.0f;
                     CGFloat y = -120.0f;
                     splashImageView.frame = CGRectMake(x,
                                                        y,
                                                        splashImageView.frame.size.width-2*x,
                                                        splashImageView.frame.size.height-2*y);
                 } completion:^(BOOL finished){
                     if (finished) {
                         [splashImageView removeFromSuperview];
                         //calling the movie after dismissing image splash
                         [self showMovieIntro];
                         
                     }
                 }];

}

-(void)showMovieIntro{
MPMoviePlayerViewController *playerVC=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Test" ofType:@"mp4"]]];

MPMoviePlayerController *player = [playerVC moviePlayer];
player.controlStyle = MPMovieControlStyleNone;   //hide the controls

//this doesnt work, returns a erros passing mpmovieplayerVC as oppose to a uiview
//[self presentModalViewController:playerViewController animated:YES];
//[self.window.rootViewController.view addSubview:playerVC];
//[self.window.rootViewController.view bringSubviewToFront:playerVC.view];

//this part right here makes the app crash
[[playerVC view] setFrame:_window.bounds];
[_window addSubview: [playerVC view]];
[_window bringSubviewToFront:[playerVC view]];

[player play];

//try using animation with duration on the movie but that was a no no...
//    [UIView animateWithDuration:1.5f delay:2.0f options:UIViewAnimationOptionCurveEaseOut animations:nil completion:^(BOOL finished) {
//        if(finished){
//            [playerVC removeFromSuperView];
//        }
//    }];
}

Solution

  • I think what you are looking for is a simple AVPlayerLayer, found under AVFoundation.framework. The way you can display a video intro after the application launches become simple with the help of this.

    First what you need is to create a root view controller that displays the video intro. Name it VideoIntroViewController. Set it as the rootViewController for the window. In the viewDidLoad section of VideoIntroController add the following lines of code:

    #import <AVFoundation/AVFoundation.h>
    
    @interface VideoIntroViewController ()
    @property (strong, nonatomic) AVPlayer *player;
    @end
    
    @implementation VideoIntroViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSURL *videoURL = [NSURL URLWithString:@"http://www.jportdev.com/MISC/Sample.mov"];
        AVPlayer* player = [AVPlayer playerWithURL:videoURL];
    
        AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
        playerLayer.frame = self.view.bounds;
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
        playerLayer.needsDisplayOnBoundsChange = YES;
    
        [self.view.layer addSublayer:playerLayer];
        self.view.layer.needsDisplayOnBoundsChange = YES;
    
        self.player = player;
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        [self.player play];
    }
    
    @end
    

    With the above code if everything goes fine the video becomes available for playing. Now in your viewDidAppear start playing the video with:

    [self.player play];
    

    All that you need after that is to monitor its status. A detail explanation of this method can be found under Apple's documentation available here.

    Hope you would find it helpful.