IOS提供了叫做MPMoviePlayerController MPMoviePlayerViewController兩個類,可以輕松用來實現視頻播放。MPMoviePlayerViewController只能全屏播放視頻。
#import "MainViewController.h" #import@interface MainViewController () //視頻播放器 @property (strong, nonatomic) MPMoviePlayerController *player; @property (strong, nonatomic) UIImageView *imageView; @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; //實例化視頻播放器 NSURL *url = [[NSBundle mainBundle]URLForResource:@"promo_full" withExtension:@"mp4"]; //視頻播放是流媒體的播放模式,所謂流媒體就是把視頻數據像流水一樣,變加載,變播放。 // //提示:如果url中包含中文,需要添加百分號。 // NSString *urlString = @"http:www.xxx.com/video/xxx.mp4"; // NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; self.player = [[MPMoviePlayerController alloc]initWithContentURL:url]; //1設置播放器的大小 [self.player.view setFrame:CGRectMake(0, 0, 320, 180)]; //16:9是主流媒體的樣式 //2將播放器視圖添加到根視圖 [self.view addSubview:self.player.view]; //4播放 [self.player play]; //[self.player stop]; //通過通知中心,以觀察者模式監聽視頻播放狀態 //1 監聽播放狀態 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; //2 監聽播放完成 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finishedPlay) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; //3視頻截圖 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(caputerImage:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil]; //3視頻截圖 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(caputerImage:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil]; //4退出全屏通知 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(exitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil]; //異步視頻截圖,可以在attimes指定一個或者多個時間。 [self.player requestThumbnailImagesAtTimes:@[@10.0f, @20.0f] timeOption:MPMovieTimeOptionNearestKeyFrame]; UIImageView *thumbnailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 160, 90)]; self.imageView = thumbnailImageView; [self.view addSubview:thumbnailImageView]; } #pragma mark 退出全屏 - (void)exitFullScreen { NSLog(@"退出全屏"); } #pragma mark -播放器事件監聽 #pragma mark 視頻截圖 這個方法是異步方法 - (void)caputerImage:(NSNotification *)notification { NSLog(@"截圖 %@", notification); UIImage *image = notification.userInfo[@"MPMoviePlayerThumbnailImageKey"]; [self.imageView setImage:image]; } #pragma mark 播放器事件監聽 #pragma mark 播放完成 - (void)finishedPlay { NSLog(@"播放完成"); } #pragma mark 播放器視頻的監聽 #pragma mark 播放狀態變化 /* MPMoviePlaybackStateStopped, //停止 MPMoviePlaybackStatePlaying, //播放 MPMoviePlaybackStatePaused, //暫停 MPMoviePlaybackStateInterrupted, //中斷 MPMoviePlaybackStateSeekingForward, //快進 MPMoviePlaybackStateSeekingBackward //快退 */ - (void)stateChange { switch (self.player.playbackState) { case MPMoviePlaybackStatePaused: NSLog(@"暫停"); break; case MPMoviePlaybackStatePlaying: //設置全屏播放 [self.player setFullscreen:YES animated:YES]; NSLog(@"播放"); break; case MPMoviePlaybackStateStopped: //注意:正常播放完成,是不會觸發MPMoviePlaybackStateStopped事件的。 //調用[self.player stop];方法可以觸發此事件。 NSLog(@"停止"); break; default: break; } } @end