Nowadays, the background of the login/registration interface of more and more apps is playing videos or GIfs. I mainly look at the login interface of Uber and Keep and make a login interface with the login interface of pull check. 1. First of all, I found these two libraries on Github: -VideoSplashKit this library is written by OC, but your login page needs to inherit this VC, borrow the following resources, copyright notice -VideoSplashKit swift version written by foreign investors, From that moment the train of thought – attach links to https://github.com/sfmDev/videoLoginDemo 2 of this article. Write a video player The guide into the < AVFoundation/AVFoundation. H >

@interface ViewController () /** * fullscreen player */ @property (strong, nonatomic) AVPlayer *player; @endCopy the code

Create a play layer,AVPlayer’s player is added to the Layer, AVPlayerLayer

- (void)setupForAVplayerView
{
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:playerLayer];
}
Copy the code
/** */ - (AVPlayer *)player {if(! _player) { AVPlayerItem *playerItem = [self getPlayItem]; _player = [AVPlayer playerWithPlayerItem:playerItem]; / / set the repeat. Play _player actionAtItemEnd = AVPlayerActionAtItemEndNone; //setThis // Notify [[NSNotificationCenter defaultCenter]addObserver:self after the video is played selector:@selector(__playerItemDidPlayToEndTimeNotification:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];  }return _player;
}
Copy the code
- (void)__playerItemDidPlayToEndTimeNotification:(NSNotification *)sender { [_player seekToTime:kCMTimeZero]; // Set to continue from the beginning}Copy the code

Set what to play

- (AVPlayerItem *)getPlayItem
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"BridgeLoop-640p" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    return playerItem;
}
Copy the code

Now get such a video playback, above the registration login is my own add

Now, the problem is that if the program cuts from foreground to background, and from background to foreground again, the video will stop playing

Application - (void) applicationDidBecomeActive: (UIApplication *) {/ / in the app into the active send notification, keep video playback [[NSNotificationCenter defaultCenter]postNotificationName:@"videoshouldplay" object:nil];
}
Copy the code

In this way, the main Settings for video playback are basically completed, and some animations can also be set. The demo I wrote imitated the pull check when logging in, but it has not reached my ideal effect for the time being, I will not write it for now, and I will talk about the animation in the login interface in the next chapter. If you are interested, welcome star