1。使用MPMoviePlayerController在当前播放的视频中异步获取屏幕截图
e.g.
@property(nonatomic,strong)UIImage *captureImg; //保留抓捕的图像
//播放视频
- (void) startPlayingVideo:(id)paramSender{
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *url = [mainBundle URLForResource:@"Sample" withExtension:@"m4v"];
if (self.moviePlayer != nil){
[self stopPlayingVideo:nil];
}
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:Nil];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if (self.moviePlayer != nil){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoHasFinishedPlaying:)
name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
//监听:抓捕帧图的操作结束时
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoThumbnailIsAvailable:)
name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:self.moviePlayer];
NSLog(@"Successfully instantiated the movie player.");
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[self.moviePlayer play];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
// -----------------------------
/* 抓捕第3帧 */
NSNumber *thirdSecondThumbnail = @3.0f;
[self.moviePlayer requestThumbnailImagesAtTimes:@[thirdSecondThumbnail] timeOption:MPMovieTimeOptionExact];
} else {
NSLog(@"Failed to instantiate the movie player.");
}
}
//将成功抓捕到的图保留,或直接进行处理
- (void) videoThumbnailIsAvailable:(NSNotification *)paramNotification{
MPMoviePlayerController *controller = [paramNotification object];
if ([controller isEqual:self.moviePlayer]){
NSLog(@"Thumbnail is available");
UIImage *thumbnail = [paramNotification.userInfo objectForKey:MPMoviePlayerThumbnailImageKey];
if (thumbnail != nil){
self.captureImg = thumbnail;
}
}
}
//停止播放,取消监听
- (void) stopPlayingVideo:(id)paramSender {
if (self.moviePlayer != nil){
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:self.moviePlayer];
[self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview];
//将抓捕到的图像进行处理
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 310, self.view.frame.size.height - 100)];
imgView.image = self.captureImg;
。。。。。。。。
}
}