#import "ViewController.h"
@import MediaPlayer;
@interface ViewController ()
{
MPMoviePlayerController *_mov;
}
@property (weak, nonatomic) IBOutlet UIImageView *xianshi;
@end
@implementation ViewController
- (IBAction)didclickdo:(id)sender {
// MPMoviePlayerController *play = [[MPMoviePlayerController alloc] initWithContentURL:@"/Users/apple/Desktop/hdmv.mp4"];
// [self presentMoviePlayerViewControllerAnimated:play];
// 已经过时的截图方式
// UIImage *image = [_mov thumbnailImageAtTime:100 timeOption:MPMovieTimeOptionNearestKeyFrame];
// _xianshi.image = image;
// 现在的截图
[_mov requestThumbnailImagesAtTimes:@[@1.0,@10.0,@30.0] timeOption:MPMovieTimeOptionNearestKeyFrame];// 截图在有效的贞上
}
-(void)didThumb:(NSNotification *)notification
{
NSLog(@"%@",notification.userInfo);//截图的具体时间
UIImage *image = notification.userInfo[MPMoviePlayerThumbnailImageKey];
if (image) {
_xianshi.image = image;//截取成功显示
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didPlaybackNotif:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didThumb:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/hdmv.mp4"];//路径
_mov = [[MPMoviePlayerController alloc]initWithContentURL:url];
_mov.view.frame = CGRectMake(100, 100, 200, 100);//播放的位置
// _mov.controlStyle = MPMovieControlStyleNone;
_mov.shouldAutoplay = NO;
[_mov prepareToPlay];//开始播放
[_mov play];
[self.view addSubview:_mov.view];
}