implement iAd video with MPMoviePlayerController with playPrerollAdWithCompletionHandler method

| | August 10, 2015

Well, I want to show a video ad just before starting a video in MPMoviePlayer

This is what I am doing:-

    moviePlayer = [MPMoviePlayerController new];

    moviePlayer.contentURL = [NSURL URLWithString:@"http://xyz/xyz.m3u8"];

    [moviePlayer playPrerollAdWithCompletionHandler:^(NSError *error) {
            // Check if error is non-nil during development
            [moviePlayer play];
        }];
    moviePlayer.view.frame=CGRectMake(0, 20, 300, self.view.frame.size.width);
    [self.view addSubview:moviePlayer.view];
    [self.view layoutIfNeeded];

And in Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [MPMoviePlayerController preparePrerollAds];
    return YES;
}

But what more should I do to show an ad video or how to configure the iAD to let the app understand which specific video to play as ad.

Currently the app is just playing this url “http://xyz/xyz.m3u8” video but not showing any ad.

One Response to “implement iAd video with MPMoviePlayerController with playPrerollAdWithCompletionHandler method”

  1. You need to prepareToPlay your MPMoviePlayerController before calling playPrerollAdWithCompletionHandler and add your MPMoviePlayerController to your view before you play it. Also, you may be playing the video before [MPMoviePlayerController preparePrerollAds] has a chance to download the video ad completely. Check my example:

    #import "ViewController.h"
    @import iAd;
    @import MediaPlayer;
    
    @interface ViewController () {
        MPMoviePlayerController *moviePlayer;
    }
    
    @end
    
    @implementation ViewController
    
    -(void)viewDidLoad {
        [super viewDidLoad];
    
        // Preload ad
        [MPMoviePlayerController preparePrerollAds];
    
        // Create our MPMoviePlayerController
        moviePlayer =[[MPMoviePlayerController alloc]init];
        [moviePlayer.view setFrame: self.view.bounds];
        [moviePlayer setFullscreen:YES animated:YES];
    }
    
    -(IBAction)playButton:(id)sender {
        // Add MPMoviePlayerController to our view
        [self.view addSubview:moviePlayer.view];
    
        // Path of movie you want to play
        NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"someVideo" ofType:@"MOV"];
    
        // Set the contents of our MPMoviePlayerController to our path
        [moviePlayer setContentURL:[NSURL fileURLWithPath:moviePath]];
    
        // Prepare our movie for playback
        [moviePlayer prepareToPlay];
    
        // Play our movie with a prerolled ad
        [moviePlayer playPrerollAdWithCompletionHandler:^(NSError *error) {
            [moviePlayer play];
        }];
    }
    

Leave a Reply