推荐播放器:
LRLAVPlayer相对易懂好修改,调整添加内容。
https://github.com/codeWorm2015/videoPlayer
NSString*path=[[NSBundle mainBundle] pathForResource:@"r00190m8et5" ofType:@"mp4"]; //@"http://f01.v1.cn/group2/M00/01/62/ChQB0FWBQ3SAU8dNJsBOwWrZwRc350-m.mp4" NSLog(@"%@",path); self.playerView=[LRLAVPlayerView avplayerViewWithVideoUrlStr:path andInitialHeight:200.0 andSuperView:self.view]; self.playerView.delegate=self; [self.view addSubview:self.playerView]; __weak WHPalyerViewController * weakSelf = self; //我的播放器依赖 Masonry 第三方库 [self.playerView setPositionWithPortraitBlock:^(MASConstraintMaker *make) { make.top.equalTo(weakSelf.view).with.offset(60); make.left.equalTo(weakSelf.view); make.right.equalTo(weakSelf.view); //添加竖屏时的限制, 这条也是固定的, 因为: _videoHeight 是float* 类型, 我可以通过它, 动态改视频播放器的高度; // make.height.equalTo(@(*(weakSelf.playerView->_videoHeight))); make.height.mas_equalTo(200); } andLandscapeBlock:^(MASConstraintMaker *make) { make.width.equalTo(@(SCREEN_HEIGHT)); make.height.equalTo(@(SCREEN_WIDTH)); make.center.equalTo(Window); }];
依赖约束,完成~
另外,此代码并不完美,需要修改,下面指出一点,需要播放本地视频的在下载的代码中修改如下:
#pragma mark - 懒加载 -(AVPlayerItem *)avplayerItem{ if (!_avplayerItem) { NSRange rang=[self.videoUrlStr rangeOfString:@"http://"]; if (rang.length) { _avplayerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.videoUrlStr]]; }else{ _avplayerItem = [AVPlayerItem playerItemWithURL:[[NSURL alloc] initFileURLWithPath:self.videoUrlStr]]; } [_avplayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; [_avplayerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; [_avplayerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; [_avplayerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; [_avplayerItem addObserver:self forKeyPath:@"playbackBufferFull" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; [_avplayerItem addObserver:self forKeyPath:@"presentationSize" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:_avplayerItem]; } return _avplayerItem; }
须知:网络播放格式:Http:// 本地播放格式:file://