一烦味、AVPlayer
能播放本地聂使、遠(yuǎn)程的音頻壁拉、視頻文件;基于Layer顯示柏靶,得自己去編寫控制面板弃理。
- 本地視頻:
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化
NSURL*url = [[NSBundlemainBundle] URLForResource:@"promo_full.mp4"withExtension:nil];
self.player = [AVPlayer playerWithURL:url];
// 設(shè)置播放圖層
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 開始播放
[self.player play];
}
@end
2.在線視頻:
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化
NSURL*url = [NSURLURLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];
self.player = [AVPlayer playerWithURL:url];
// 設(shè)置播放圖層
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 開始播放
[self.player play];
}
@end
注意:
播放視頻時(shí)需要設(shè)置播放視頻圖層。
二屎蜓、MPMoviePlayerController
能播放本地蚁廓、遠(yuǎn)程的音頻凸郑、視頻文件,YouTobe就是用MPMoviePlayerController實(shí)現(xiàn)的
它繼承自NSObject,自帶播放控制面板(暫停、播放徽鼎、播放進(jìn)度、是否要全屏)
MPMoviePlayerController可以播放的視頻格式包括:H.264往枷、MPEG-4等蝌矛;支持的文件擴(kuò)展名包括:avi,mkv,mov,m4v,mp4等
此類定義在了MediaPlayer框架
加載視頻資源(注意,如果url為nil同樣可以加載)
NSAssert(self.url, @"URL不能為空");
[[MPMoviePlayerController alloc] initWithContentURL:self.url];顯示 :[self.view addSubview:self.moviePlayer.view];
通過設(shè)置AutoresizingMask屬性可以在橫豎屏轉(zhuǎn)換時(shí)自動調(diào)整視圖大小播放:[self.moviePlayer play];
全屏 :[self.moviePlayer setFullscreen:YES animated:YES];
MPMoviePlayerController的播放狀態(tài)是通過通知中心監(jiān)聽的荐吵,常用監(jiān)聽通知事件
狀態(tài)變化
MPMoviePlayerPlaybackStateDidChangeNotification播放結(jié)束
MPMoviePlayerPlaybackDidFinishNotification退出全屏
MPMoviePlayerDidExitFullscreenNotification截屏完成
MPMoviePlayerThumbnailImageRequestDidFinishNotification截屏方法
-requestThumbnailImagesAtTimes:timeOption:
- 代碼實(shí)現(xiàn)
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
/**
* 視頻播放器
*/
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建視頻播放器
NSURL *url = [[NSBundle mainBundle] URLForResource:@"promo_full.mp4" withExtension:nil];
// NSURL *url = [NSURL URLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 設(shè)置播放顯示視圖
self.moviePlayer.view.frame = self.view.bounds;
[self.view addSubview:self.moviePlayer.view];
// 使用autoLayout適配橫豎屏
// 1.禁用translatesAutoresizingMaskIntoConstraints
self.moviePlayer.view.translatesAutoresizingMaskIntoConstraints = NO;
// 2.設(shè)置約束
NSArray *constraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[moviePlayerView]-0-|" options:0 metrics:nil views:@{@"moviePlayerView" :self.moviePlayer.view}];
NSArray *constraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[moviePlayerView]-0-|" options:0 metrics:nil views:@{@"moviePlayerView" :self.moviePlayer.view}];
[self.view addConstraints:constraintH];
[self.view addConstraints:constraintV];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 兩種方式都是直接播放
// 播放視頻
[self.moviePlayer play];
// 準(zhǔn)備后直接播放
// [self.moviePlayer prepareToPlay];
}
@end
三骑冗、MPMoviePlayerViewController
- 能播放本地、遠(yuǎn)程的音頻先煎、視頻文件
- 內(nèi)部是封裝了MPMoviePlayerController
- 播放界面默認(rèn)就是全屏的
- 如果播放功能比較簡單贼涩,僅僅是簡單地播放遠(yuǎn)程、本地的視頻文件榨婆,建議用這個
- 此類定義在了MediaPlayer框架
- 代碼實(shí)現(xiàn)
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 創(chuàng)建播放控制器
NSURL*url = [NSURLURLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];
MPMoviePlayerViewController*moviePlayerVc = [[MPMoviePlayerViewControlleralloc] initWithContentURL:url];
// 播放視頻
[self presentMoviePlayerViewControllerAnimated:moviePlayerVc ];
}
@end
注意:
使用MPMoviePlayerViewController時(shí)創(chuàng)建的視頻播放控制器不需要強(qiáng)引用磁携。