Allen Allen - 6 months ago 40
Swift Question

How to Play a mp4 Video on the launch screen when the app loads Xcode SWIFT

Hey I have a Mp4 video that i won't to put on the launch screen. Like who when on image loads on the launch screen but now just a video. And i do know how to put a image on the launch screen but not a video any help?

Answer

You can't put a video on the launch screen, but can start a video as soon as the apple starts. Visually, the effect can be undistinguishable.

Just add the video play call on the viewDidLoad method of your first view controller, and place in some control not to force the user to view it all the times the app is run. Otherwise your users will get annoyed soon.

Yom might want to adapt the first photogram of the video to your launch screen, and provide different videos for several device resolutions. Keep in mind that the various video sizes of supported by the various mobile apple devices don't exactly always match the display size of each devices.

Your mileage may vary, you will need to "rotate" your videos in such a way that it appears to be "vertical" when played in horizontal.

E.g.:

- (void) play{
NSString *videoName=@"video320";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        videoName=@"vid_trailer";
        CGFloat height =self.view.frame.size.height;

        //test retina display
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
            // RETINA DISPLAY - you may need to add check for @3x resolutions
            videoName=@"video920";
        }

        if (height > 460.0){
            videoName=@"video1096";
        }
    } else {
        videoName=@"video768";
        self.view.backgroundColor=[UIColor blackColor];
    }
        NSString *urlString = [[NSBundle mainBundle]     pathForResource:videoName ofType:@"mp4"];
    NSLog(@"urlString=%@",urlString);

    NSURL *url = [NSURL fileURLWithPath:urlString];
    if (self.videoPlayer != nil)
    {
        [self.videoPlayer stop];
        self.videoPlayer=nil;
    }
    self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    self.videoPlayer.view.frame=self.view.frame;


self.videoPlayer.fullscreen=NO;
[self.videoPlayer setScalingMode:MPMovieScalingModeAspectFill];
[self.videoPlayer setShouldAutoplay:YES];
[self.videoPlayer setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(terminateVideo:)
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:nil];
for(UIView* subV in self.videoPlayer.view.subviews) {
    subV.backgroundColor = [UIColor clearColor];
}
[self.tabBarController.view addSubview:self.videoPlayer.view];
[self.navigationController.view addSubview:self.videoPlayer.view];
[self.videoPlayer play];

}

- (void) terminateVideo:(id)sender{
     [self.videoPlayer.view removeFromSuperview];
     self.videoPlayer =nil;
     self.view.backgroundColor=[UIColor darkGrayColor];
 }