Search code examples
iphonexcodempmovieplayercontrollernsnotifications

How to target NSNotification for different MPMoviePlayerControllers?


I'm trying to create an app that displays a unique info page after different videos play. Currently, I am displaying the info page with the moviePlayBackDidFinish notification method but I can't figure out how to customize it for different videos. Here's my code...thanks very much in advance!!

EDIT after subclassing movieplayercontroller...how do I use new property in NSNotification?

//subclassed movieplayercontroller myMovie.h

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface myMovie: MPMoviePlayerViewController
{
    myMovie *videoPlayer;
}

@property (nonatomic, strong) NSString *movieTitle;

@end

myMovie.m

#import "myMovie.h"

@interface myMovie ()

@end

@implementation myMovie

@synthesize movieTitle;

@end

//main viewcontroller

videoPlayViewController.h

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "View2.h"
#import "myMovie.h"

@interface videoPlayViewController : UIViewController


-(IBAction) playMovie;

videoPlayViewController.m

#import "videoPlayViewController.h"
#import "myMovie.h"

@interface videoPlayViewController ()

@end


@implementation videoPlayViewController


-(void)playMovie
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                         pathForResource:@"sample" ofType:@"mov"]];

   myMovie *videoPlayer =  [[myMovie alloc]
                    initWithContentURL:url];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:videoPlayer.moviePlayer];




   videoPlayer.moviePlayer.controlStyle = MPMovieControlStyleDefault;
    videoPlayer.moviePlayer.shouldAutoplay = NO;
    videoPlayer.movieTitle = @"sample";
    [self.view addSubview:videoPlayer.view];
    [videoPlayer.moviePlayer setFullscreen:YES animated:YES];



}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    NSLog(@"Is this working?");

    View2 *second =[[View2 alloc] initWithNibName:nil bundle:nil];

    [self presentModalViewController:second animated:YES];

    [player.view removeFromSuperview];

}

Solution

  • Create a subclass of MPMoviePlayerController with a property (or many properties) to hold the info that you need when the video ends. Then, when the video ends, you'll get your customized MPMoviePlayerController in the notification object, and you can inspect the properties to find out whatever it was that you wanted to know about the movie that ended.