MPMovieplayer は、この目的には適していません。AVPlayer (AVFoundation.framework の下にあります) を使用して、目的に合ったカスタム ムービー プレーヤーを作成できます。プロジェクトに通常の ViewController を作成し、以下のようなコードで AVPlayer を追加します。
-(void)viewDidLoad {
//prepare player
self.videoPlayer = [AVPlayer playerWithURL:<# NSURL for the video file #>];
self.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;
//prepare player layer
self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
self.videoPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.videoPlayerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.videoPlayerLayer];
//add player item status notofication handler
[self addObserver:self forKeyPath:@"videoPlayer.currentItem.status" options:NSKeyValueObservingOptionNew context:NULL];
//notification handler when player item completes playback
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
}
//called when playback completes
-(void)playerItemDidReachEnd:(NSNotification *)notification {
[self.videoPlayer seekToTime:kCMTimeZero]; //rewind at the end of play
//other tasks
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"videoPlayer.currentItem.status"]) {
//NSLog(@"player status changed");
if (self.videoPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay) {
//player is ready to play and you can enable your playback buttons here
}
}
}
これは通常のビュー コントローラーであるため、再生/共有などのためにツールバー ボタン/ボタンを追加し、以下のようなプレーヤー アクションおよびその他の関連アクションをトリガーできます。
-(IBAction)play:(id)sender {
[self.videoPlayer play];
}
-(IBAction)pause:(id)sender {
[self.videoPlayer pause];
}
//etc.
また、dealloc でオブザーバーを必ず削除してください。
-(void)dealloc {
//remove observers
@try {
[self removeObserver:self forKeyPath:@"videoPlayer.currentItem.status" context:NULL];
}
@catch (NSException *exception) {}
@try {
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
}
@catch (NSException *exception) {}
//other deallocations
[super dealloc];
}
プロセスのより詳細で洗練された説明は、こちらから入手できるりんごのコンパニオン ガイドにあります。