A practical guide. Local MP4 video as background in iOS app. From iOS 8

I am trying to use a simple 13 second mp4 video as a background loop for my login screen.

I want the video to be automatic and looping. It has no audio and I don't need any controls.

I need buttons and other objects in front of it.

I tried to use WebView and make an MP4 GIF file from this tutorial: https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2

But the problem is my 5MB MP4 is (converted to GIF) 95MB in size.

I cannot use this method.

Is there a "Ease of Use" way?

EDIT:

Ok, this is what I have done now.

I've imported AVFoundation.

This is the code in the view

- (void)viewDidLoad {
[super viewDidLoad];

NSURL* mURL = [[NSBundle mainBundle] URLForResource:@"App-BG-Loop" withExtension:@"mp4"];

AVPlayer* player = [AVPlayer playerWithURL:mURL];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[player currentItem]];

AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = _videoView.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerLayer.needsDisplayOnBoundsChange = YES;
[_videoView.layer addSublayer:playerLayer];

_videoView.layer.needsDisplayOnBoundsChange = YES;

[player play];

// Do any additional setup after loading the view, typically from a nib.}

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

      

This works fine in the iOS Simulator, but when I try to run it on the device it won't start and play the video. The look just stays white. There is no error.

Any idea?

EDIT 2:

The problem with not playing on the device was the size of the video, which was large. Apple only supports 1080p on devices.

+3


source to share


1 answer


I recently had to use animation too. I tried it with at first UIImageView

, but the memory management was not very good. I ended up creating .mp4 and used it in MPMoviePlayerController

:



-(void)setupDashboardAnimation
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(checkMovieStatus:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:nil];

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"some_movie" ofType:@"some_extention"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

    _backgroundAnimationMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    _backgroundAnimationMoviePlayer.controlStyle = MPMovieControlStyleNone;
    _backgroundAnimationMoviePlayer.repeatMode = MPMovieRepeatModeOne;
    _backgroundAnimationMoviePlayer.view.backgroundColor = [UIColor clearColor];
    for (UIView *aSubView in _backgroundAnimationMoviePlayer.view.subviews)
    {
        aSubView.backgroundColor = [UIColor clearColor];
    }
    [_backgroundAnimationMoviePlayer.view setFrame:imv_background.frame];
    [_backgroundAnimationMoviePlayer prepareToPlay];
}

-(void)checkMovieStatus:(NSNotification *)notification
{
    if (_backgroundAnimationMoviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK) && _backgroundAnimationMoviePlayer.playbackState != MPMoviePlaybackStatePlaying)
    {
        [self.view insertSubview:_backgroundAnimationMoviePlayer.view aboveSubview:imv_background];
        [_backgroundAnimationMoviePlayer play];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerLoadStateDidChangeNotification
                                                      object:nil];
        dashboardAnimationDidSetup = YES;
    }
}

      

+1


source







All Articles