关于流媒体视频
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> /** http://baobab.cdn.wandoujia.com/14468618701471.mp4 http://hls.quanmin.tv/live/35743/playlist.m3u8 */ static const NSString *PlayerItemStatusContext; @interface ViewController () @property (nonatomic, strong) AVPlayer *player; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // //1. 根据网址创建AVPlayer // self.player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://hls.quanmin.tv/live/35743/playlist.m3u8"]]; // // //2. 创建PlayerLayer // AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; // // //3. 设置大小 // playerLayer.frame = self.view.bounds; // // //4. 添加到layer中 // [self.view.layer addSublayer:playerLayer]; // // //5. 播放 // [self.player play]; // } - (IBAction)playLocalVideoClick:(id)sender { NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"02开发环境.mp4" withExtension:nil]; // // AVAsset *asset = [AVAsset assetWithURL:assetURL]; // // AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset]; // // [playerItem addObserver:self forKeyPath:@"status" options:0 context:&PlayerItemStatusContext]; // // self.player = [AVPlayer playerWithPlayerItem:playerItem]; self.player = [AVPlayer playerWithURL:assetURL]; AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; playerLayer.frame = self.view.bounds; [self.view.layer addSublayer:playerLayer]; [self.player play]; } - (IBAction)playRomoteVideoClick:(id)sender { //1. AVPlayerItem会创建媒体资源动态视角的数据模型(比如当前播放时间, 实现时间跳转等), 并保存AVPlayer在播放资源时的呈现状态 AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://hls.quanmin.tv/live/35743/playlist.m3u8"]]; [playerItem addObserver:self forKeyPath:@"status" options:0 context:&PlayerItemStatusContext]; self.player = [AVPlayer playerWithPlayerItem:playerItem]; AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; playerLayer.frame = self.view.bounds; [self.view.layer addSublayer:playerLayer]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context { if (context == &PlayerItemStatusContext) { AVPlayerItem *playerItem = (AVPlayerItem *)object; if (playerItem.status == AVPlayerItemStatusReadyToPlay) { [self.player play]; } } } - (IBAction)mpVCPlayRomoteClick:(id)sender { MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://baobab.cdn.wandoujia.com/14468618701471.mp4"]]; [self presentModalViewController:mpvc animated:YES]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // [self.player play]; }
这是一个小demo的实现
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController () @property(nonatomic,strong)AVPlayer *player;//播放对象 @property(nonatomic,strong)MPMoviePlayerController *mc; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } /** http://baobab.cdn.wandoujia.com/14468618701471.mp4 http://hls.quanmin.tv/live/35743/playlist.m3u8 */ - (IBAction)clik:(id)sender { //1mpc: // self.mc=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:@"http://baobab.cdn.wandoujia.com/14468618701471.mp4"]]; // self.mc.view.frame=CGRectMake(0, 200, 375, 200); // [self.view addSubview:self.mc.view]; // [self.mc play]; //2mpvc // MPMoviePlayerViewController *mpv=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:@"http://baobab.cdn.wandoujia.com/14468618701471.mp4"]]; // [self presentViewController:mpv animated:YES completion:nil]; //3Aplayer //根据网址创建 self.player=[AVPlayer playerWithURL:[NSURL URLWithString:@"http://baobab.cdn.wandoujia.com/14468618701471.mp4"]]; AVPlayerLayer *player=[AVPlayerLayer playerLayerWithPlayer:self.player]; player.frame=self.view.bounds; [self.view.layer addSublayer:player]; //播放 [self.player play]; }