Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulating splash video in iOS application

OK, there are very few options to emulate the splash video in iOS. All we can do is wait till application is fully loaded and then create Media Player and load video in it.

I implemented it with following code:

-(void) moviePlayBackDidFinish:(NSNotification*)notification
{
    NSLog(@"Intro video stopped");
    [mMoviePlayer release];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSURL* mMovieURL;
    NSBundle *bundle = [NSBundle mainBundle];
    if(bundle != nil)
    {
        NSString *moviePath = [bundle pathForResource:@"intro" ofType:@"mp4"];
        if (moviePath)
        {
            mMovieURL = [NSURL fileURLWithPath:moviePath];
            [mMovieURL retain];
        }
    }

    mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mMovieURL];
    [mMovieURL release];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:mMoviePlayer];

    mMoviePlayer.controlStyle = MPMovieControlStyleNone;
    [mMoviePlayer.backgroundView addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Splash/background.png"]] autorelease]];
    mMoviePlayer.scalingMode = MPMovieScalingModeFill;

    [window addSubview:mMoviePlayer.view];
    [mMoviePlayer setFullscreen:YES animated:NO];

    [window makeKeyAndVisible];
    [mMoviePlayer play];

<... other stuff ...>

}

My video is only 1 MB. But this code do something different then I'd like to see:

  1. First of all user can see a static splash screen for a few seconds;
  2. Then a black screen appears for 1 or 2 seconds. I think this is happening because the media player is loaded.
  3. Video start playing.
  4. Main interface loads.

As you understand I don't like the pause with black screen - it looks ugly.

As far as I can see in my Console log the problem is that mediaplayer is waiting till the main view controller is fully loaded.

Few words about main view: i'm writing an application for iPad and the main view consists of several subviews with multiple images. Every image and every subview in main view loads some data from Internet Web service via ASIHTTPRequest lib.

I think that Media Player is waiting for all initial connections to finish and only then it's starting the video...

How can I force the video to play before main view is loaded? Or maybe I can delay the loading of main XIB?

like image 484
WASD42 Avatar asked Aug 13 '11 14:08

WASD42


2 Answers

You cannot get rid of the static splash image. While it is shown, the OS is loading the application and instantiating stuff until it is ready to call your UIApplicationDelegate. So all you can do is either use no splash (black screen for a few seconds) or make your movie start exactly with the shown splash screen so it looks like the static image would suddenly animate.

To get rid of the black screen while the movie loads, you can try to make the player transparent and have an UIImageView behind the player that shows the splash image. The behavior would be this:

  • Splash screen is shown (static image).
  • Application is loaded. You see the UIImageView, also showing the splash screen. On top of it is the transparent movie player.
  • Movie player finally has loaded the move and starts playing it.

At least in theory, this should cause the effect that the static image suddenly starts animating.

But if you don't use a splash screen at all (a lot of games do that), then it doesn't matter that the movie player is showing a black screen at first, you wouldn't notice.

Regarding showing the splash screen in an UIImageView: unfortunately, you have to test the interface rotation and load the image manually, there's no way to query which splash screen was shown. If you only support one interface orientation (again, a lot of games do this) you don't have this problem, of course.

like image 68
DarkDust Avatar answered Oct 16 '22 19:10

DarkDust


There is a better solution now, assuming you are using UIViewControllers.

Instead of using MPMoviePlayerController, use MPMoviePlayerViewController. Here is some sample code, adapted from the question:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSURL* mMovieURL;
    NSBundle *bundle = [NSBundle mainBundle];
    if(bundle != nil)
    {
        NSString *moviePath = [bundle pathForResource:@"intro" ofType:@"mp4"];
        if (moviePath)
        {
            mMovieURL = [NSURL fileURLWithPath:moviePath];
            [mMovieURL retain];
        }
    }

    mMoviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:mMovieURL];
    [mMovieURL release];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:mMoviePlayer.moviePlayer];
    mMoviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
    [mMoviePlayer.moviePlayer.backgroundView  addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SplashCopy.png"]] autorelease]];
    mMoviePlayer.moviePlayer.scalingMode = MPMovieScalingModeFill;
    [window.rootViewController.view addSubview:mMoviePlayer.moviePlayer.view];
    [mMoviePlayer.moviePlayer setFullscreen:YES animated:NO];
    [mMoviePlayer.moviePlayer play];

}

like image 42
supadenz Avatar answered Oct 16 '22 20:10

supadenz