Saturday 15 November 2014

VideoPlayer Open when App is Launched

Add the Framework File into Project : MediaPlayer.framework 

AppDelegate.h File

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@class MPMoviePlayerController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    MPMoviePlayerController *moviePlayer;
    
    UIView *view_background;
}

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m File

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController *ViewControl = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController: ViewControl];
    self.window.rootViewController = detailNavigationController;
    [self.window makeKeyAndVisible];


view_background = [[UIView alloc]init];
    
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@“video_Name” ofType:@"mp4"]]];
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        view_background.frame = CGRectMake(0, 0, 1024, 768);
        moviePlayer.view.frame = CGRectMake(112, 170, 800, 427);
    }
    else
    {
        NSString *version = [[UIDevice currentDevice]systemVersion];
        float ver = [version floatValue];
        
        if (ver >= 8.0)
        {
            moviePlayer.view.frame = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height);
        }
        else
        {
            moviePlayer.view.frame = CGRectMake(0, 0, self.window.frame.size.height, self.window.frame.size.width);
        }
        
        view_background.frame = moviePlayer.view.frame;
    }
    view_background.backgroundColor = [UIColor whiteColor];
    moviePlayer.backgroundView.backgroundColor = [UIColor whiteColor];
    [moviePlayer setControlStyle:MPMovieControlStyleNone];
    [moviePlayer setFullscreen:YES animated:YES];
    moviePlayer.shouldAutoplay = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer];
    [moviePlayer play];
    [view_background addSubview:moviePlayer.view];
    [detailNavigationController.view addSubview:view_background];
    
    return YES;
}


-(void) moviePlayBackDidFinish:(NSNotification*)notification
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:1.0];
    [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
         {
             view_background.frame = CGRectMake(-1024, 0, 1024, 768);
         }
         else
         {
             view_background.frame = CGRectMake(-568, 0, 568, 320);
         }
     }
                     completion:^(BOOL finished)
     {
         NSLog(@"Completed");
         [moviePlayer.view removeFromSuperview];
         [view_background removeFromSuperview];
     }];
    [UIView commitAnimations];
}


No comments: