こんにちは、プログラマのLFです。

今回の記事は自分のための備忘録的な意味合いで書いています。 

MPMoviePlayerControllerで動画を再生させたのですが
再生完了時の通知が発生しないという問題に遭遇しました。

結論からいうと、生成したMPMoviePlayerControllerを保持するためにプロパティを宣言して
MPMoviePlayerControllerインスタンスを保持させておかないとダメみたいです。

ViewController.m

@interface ViewController ()
@property (strong, nonatomic) MPMoviePlayerController *movPlayer;
//インスタンスを保持する変数(これがないとdelegateが動作しない)
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

(省略)

self.movPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movURL];

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

}


//再生終了後(MPMoviePlayer delegate)
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
MPMoviePlayerController* moviePlayerObj = [notification object];
[moviePlayerObj.view removeFromSuperview];
}

MPMoviePlayerControllerは過去に何度も使っているのに、
とってもいまさらなことで悩んでしまう辺り、
まだまだ修行が足りませんね…。