iOS提供了叫作MPMoviePlayerController、MPMoviePlayerViewController的兩個類,能夠用來輕鬆播放視頻app
YouTobe就是用MPMoviePlayerController實現的框架
MPMoviePlayerViewController只能全屏播放視頻ide
上述兩個類都定義在了MediaPlayer框架中測試
MPMoviePlayerController繼承自NSObject,它內部有個view用來展現視頻內容,添加其餘控制器的view上面便可顯示atom
MPMoviePlayerController能夠播放的視頻格式有如下兩種:url
H.264spa
MPEG-4 Part 2 video視頻
支持的文件拓展名爲:avi,mkv,mov,m4v,mp4等server
能夠到蘋果官網:http://support.apple.com/kb/HT1425下載一些用來測試的視頻文件,文件都比較小繼承
// 加載視頻資源
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"sample_iTunes" ofType:@"mov"];
NSURL *url = [NSURL fileURLWithPath:urlString];
// 建立播放器
_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 設置尺寸
_player.view.frame = self.view.bounds;
_player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// 添加到控制器的view上
[self.view addSubview:_player.view];
// 播放
[_player play];
// 監聽播放狀態的改變
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:_player];
// 監聽播放器結束全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitFullscreen) name:MPMoviePlayerDidExitFullscreenNotification object:_player];
是否要自動播放
@property(nonatomic) BOOL shouldAutoplay;
全屏顯示
@property(nonatomic, getter=isFullscreen) BOOL fullscreen;
- (void)setFullscreen:(BOOL)fullscreen animated:(BOOL)animated;
截取視頻中的圖片
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option;
MPMoviePlayerViewController繼承自UIViewController,它內部封裝了一個MPMoviePlayerController
MPMoviePlayerViewController只能全屏播放
簡單使用:
MPMoviePlayerViewController *play = [[MPMoviePlayerViewController alloc] initWithContentURL:url];