28 Jun 2018

How to add video as background in IOS SDK


Introduction

In this post, we will learn how to add video as background in our applications. For this we can use AVPlayer control. Let's us start, first create a new project with single view application and name it "BackgroundVideo-Demo".

Implementation of code

Open your ViewController.m file and create a method named addMoviePlayer.  Inside this method we will add our AVPlayer code. Drag the video you want to play as background into your project. Import AVFoundation.framework


#import AVFoundation/AVFoundation.h


You can download one from given below link

Download Video

First we need to get path of our video  , we can get it by pathForResource method, then we create URL by initializing it with the video path.


   NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"NycTraffic" ofType:@"mp4"];
    NSURL *videoURL = [[NSURL alloc] initFileURLWithPath: videoPath];


Now create an instance of AVPlayerItem and pass our videoURL to it.


    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:[AVURLAsset URLAssetWithURL:videoURL options:nil]];


Create your videoPlayer using AVPlayer and intilaise it with playerItem.


    AVPlayer* videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];



Finally we create layer of our player and give it a frame, add it to our view layer.


  AVPlayer* videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer* videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];
    [videoPlayerLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view.layer addSublayer:videoPlayerLayer];
    [videoPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];



 Additionally we set our player to mute, so that no sounds will play.



    [videoPlayer setMuted:YES];


Call  addMoviePlayer method in viewDidLoad.

Run your code, you will see player playing the video. But since there is no repeat node available in AVPlayer. Our video will play once, so we have to check when our video stopped and at that particular event we have to restart it. For this we will add notification handler





 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification


                                               object:[videoPlayer currentItem]];



- (void)playerItemDidReachEnd:(NSNotification *)notification {
    AVPlayerItem *p = [notification object];
    [p seekToTime:kCMTimeZero];


}


Complete Code:



-(void)addMoviePlayer{
    
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"NycTraffic" ofType:@"mp4"];
    NSURL *videoURL = [[NSURL alloc] initFileURLWithPath: videoPath];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:[AVURLAsset URLAssetWithURL:videoURL options:nil]];
    AVPlayer* videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer* videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];
    [videoPlayerLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [videoPlayer setMuted:YES];
    videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[videoPlayer currentItem]];
    [self.view.layer addSublayer:videoPlayerLayer];
    [videoPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [videoPlayer play];

}

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    AVPlayerItem *p = [notification object];
    [p seekToTime:kCMTimeZero];


}


Now run your code you will see video loops in the background.

No comments:

Post a Comment