Objective-C (iOS): MOV 動画の再生

MovieViewController

事前に MediaPlayer.framework をリンクしてください。

Objective-C: MovieViewController.h

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

@interface MovieViewController : UIViewController

@property (nonatomic, strong) MPMoviePlayerController *moviePlayerController;

@end

Objective-C: MovieViewController.m

#import "MovieViewController.h"

@interface MovieViewController ()

@end

@implementation MovieViewController

@synthesize moviePlayerController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // MOV
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mov"]];
    
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    moviePlayer.view.frame = CGRectMake(0,0,320,480);
    moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
    [self.view addSubview:moviePlayer.view];
    [moviePlayer prepareToPlay];
    [moviePlayer play];
    
    // メンバとして保持する
    moviePlayerController = moviePlayer;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinished:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
}

- (void)moviePlayBackDidFinished:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    switch([[userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"playback ended");
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"playback error");
            break;
    }
}

@end