How To Loop FullScreen Video On IOS

Hello,
Thank you very much for your time and wisdom! I am trying to loop a fullscreen video on IOS indefinitely, and only close the video once a user has touched the screen. I got the movie to play fine and cancel on input, but there doesn’t seem to be any documentation on making the video loop. The default behavior is for it to just play once and close automatically.

I’m using:
PlayFullScreenMovie(path, Color, FullScreenMovieControlMode.CancelOnInput)

How can I implementing a looping movie on IOS?

My solution may be a little long winded. Would love to see some more straightforward code, Objective-C really confuses me so I had to do a lot of trial and error. Also to my knowledge there is no way to do this yet within Unity. It needs to be done inside Xcode project.

This solution also removes the sliding animation for when the movie file comes up.

  1. Get JRSwizzle.h and JRSwizzle.m from github. Place both files in your Classes folder inside the Xcode project.

  2. Generate a file called UIViewController.h and place it in the Classes folder as well. Put the following code in UIViewController.h

    #import "JRSwizzle.h"
    #import "VideoViewController.h"
    
    @interface UIViewController(OverrideAnimatedTransition)
    - (void) nonanimatedPresentMoviePlayerViewControllerAnimated (MPMoviePlayerViewController*) moviePlayerViewController;
    - (void) nonanimatedDismissMoviePlayerViewControllerAnimated;
    @end
    @implementation UIViewController(OverrideAnimatedTransition)
    - (void) nonanimatedPresentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController*) moviePlayerViewController {
        [self presentModalViewController:moviePlayerViewController animated:NO];
    }
    - (void) nonanimatedDismissMoviePlayerViewControllerAnimated {
        [self dismissModalViewControllerAnimated:NO];
    }
    @end
    
  3. Locate the class AppController.mm and put the following code inside “didFinishLaunchingWithOptions():”

    NSError* err = nil;
     
    [UIViewController jr_swizzleMethod:@selector(presentMoviePlayerViewControllerAnimated:) withMethod:@selector(nonanimatedPresentMoviePlayerViewControllerAnimated:) error:&err];
        [UIViewController jr_swizzleMethod:@selector(dismissMoviePlayerViewControllerAnimated) withMethod:@selector(nonanimatedDismissMoviePlayerViewControllerAnimated) error:&err];
    

and put these two lines at the top of AppController.mm

	#import "JRSwizzle.h"
	#import "UIViewController.h"

That should work! Good Luck!

The line below is responsible for looping the video. It’s in the UIViewController.h code above.

moviePlayerViewController.moviePlayer.repeatMode = MPMovieRepeatModeOne;

PlayFullScreenMovie is actually implemented in FullScreenVideoPlayer.mm under iOS project.

There are 2 classes to consider:

AVKitVideoPlayback

Inside actuallyStartTheMovie:(NSURL*)url function add:

videoViewController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[videoViewController.player currentItem]];

Under onPlayerDidFinishPlayingVideo function:

remove [self finish];

Under - (void)finish

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:AVPlayerItemDidPlayToEndTimeNotification
                                                  object:[videoViewController.player currentItem]];

Add new function:

- (void)playerItemDidReachEnd:(NSNotification *)notification
{

  AVPlayerItem *p = [notification object];
  [p seekToTime:kCMTimeZero];
  [videoViewController.player play];
}

MPVideoPlayback (for old iOS)

Under - (void)actuallyStartTheMovie:(NSURL*)url function

moviePlayer.repeatMode = MPMovieRepeatModeOne;