1,使用文件路径初始化NSURL对象
NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
moviePlay = [[MPMoviePlayerController alloc] initWithContentURL:url];
2,异步抓屏
a,首先添加观察者,并实现观察到后的处理方法。
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didReceived:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
b,调用异步抓屏方法,抓取完毕后,发送相应通知。
[movieViewCtrl.moviePlayer requestThumbnailImagesAtTimes:array timeOption:MPMovieTimeOptionExact];
[[NSNotificationCenter defaultCenter] postNotificationName:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
c,处理方法的具体实现
- (void)didReceived:(NSNotification *)notif
{
UIImage *catchPic = [notif.userInfo objectForKey:MPMoviePlayerThumbnailImageKey];
//采用switch并不好,因为switch的参数只能是整数。
if (catchPic) {
switch ([[notif.userInfo objectForKey:MPMoviePlayerThumbnailTimeKey] intValue]) {
case 2:
self.catchImageView.image = catchPic;
[self.view addSubview:_catchImageView];
break;
case 8:
self.secondCatchImageView.image = catchPic;
[self.view addSubview:_secondCatchImageView];
break;
default:
break;
}
}
}