ios视频相关iOS学习开发iOS学习笔记

iOS开发--视频播放

2016-12-15  本文已影响422人  一个有个性的女汉子

很多app中都有视频播放的功能,今天做了一个视频播放,基于AVPlayer,支持横屏、竖屏(全屏播放还可锁定屏幕方向),上下滑动调节音量、屏幕亮度,左右滑动调节播放进度。

github地址是:https://github.com/v5yingying/WatchVideo.git

因为我的模拟器一直启动不起来,所以今天就不录制小视频了,放一张图片:

今晚更新一下xcode,希望明天能录制小视频给你们看,下面上代码:

首先创建cell,

- (void)awakeFromNib {

[superawakeFromNib];

self.selectionStyle=UITableViewCellSelectionStyleNone;

[selflayoutIfNeeded];

self.bgImgView.tag=101;

self.bgImgView.userInteractionEnabled=YES;

self.playBtn= [UIButtonbuttonWithType:UIButtonTypeCustom];

[self.playBtnsetImage:[UIImageimageNamed:@"video_list_cell_big_icon"]forState:UIControlStateNormal];

[self.playBtnaddTarget:selfaction:@selector(playVideo:)forControlEvents:UIControlEventTouchUpInside];

[self.bgImgViewaddSubview:self.playBtn];

[self.playBtnmas_makeConstraints:^(MASConstraintMaker*make) {

make.center.equalTo(self.bgImgView);

make.width.height.mas_equalTo(50);

}];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[supersetSelected:selectedanimated:animated];

}

- (void)playVideo:(UIButton*)sender{

if(self.playBlock) {

self.playBlock(sender);

}

}

然后创建controller,

@interfaceYYWatchVideoViewController()

@property(nonatomic,strong)UITableView*tableView;

@property(nonatomic,strong)ZFPlayerView*playerView;

@property(nonatomic,strong)ZFPlayerModel*playerModel;

@end

@implementationYYWatchVideoViewController

#pragma mark - lifeCycle

- (void)viewDidLoad {

[superviewDidLoad];

[selfinitSubviews];

}

#pragma mark -设置UI

- (void) initSubviews {

self.view.backgroundColor= [UIColorwhiteColor];

[selfsetupTableView];

[self.viewaddSubview:self.tableView];

//设置约束

[self.tableViewmas_makeConstraints:^(MASConstraintMaker*make) {

make.left.right.mas_equalTo(self.view);

make.top.mas_equalTo(self.view).mas_offset(64);

make.height.mas_equalTo(self.view.bounds.size.height);

}];

}

#pragma mark -设置tableview

- (void) setupTableView{

self.tableView.backgroundColor= [UIColorwhiteColor];

self.tableView.dataSource=self;

self.tableView.delegate=self;

self.tableView.estimatedRowHeight=379.0f;

self.tableView.rowHeight=UITableViewAutomaticDimension;

[self.tableViewregisterNib:[UINibnibWithNibName:@"YYWatchVideoCell"bundle:nil]forCellReuseIdentifier:watchCellID];

}

- (void)viewWillDisappear:(BOOL)animated{

[superviewWillDisappear:animated];

[self.playerViewresetPlayer];

}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

return10;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

YYWatchVideoCell*cell = [tableViewdequeueReusableCellWithIdentifier:watchCellID];

__blockNSIndexPath*weakIndexPath = indexPath;

__blockYYWatchVideoCell*weakCell = cell;

cell.playBlock= ^(UIButton*btn){

NSURL*videoURL = [NSURLURLWithString:@"http://files.selfimg.com.cn/media/video/2016/11/18/11ee13997555832e38f169b6d4f14bfa.mp4"];

self.playerModel= [[ZFPlayerModelalloc]init];

self.playerModel.videoURL= videoURL;

self.playerModel.tableView=self.tableView;

self.playerModel.indexPath= weakIndexPath;

self.playerModel.cellImageViewTag= weakCell.bgImgView.tag;

self.playerView.playerModel=self.playerModel;

[self.playerViewaddPlayerToCellImageView:weakCell.bgImgView];

[self.playerViewautoPlayTheVideo];

};

returncell;

}

#pragma mark - lazy

- (UITableView*)tableView{

if(_tableView==nil) {

_tableView= [[UITableViewalloc]init];

}

return_tableView;

}

- (ZFPlayerView*)playerView{

if(_playerView==nil) {

_playerView= [ZFPlayerViewsharedPlayerView];

_playerView.delegate=self;

ZFPlayerControlView*controlView = [[ZFPlayerControlViewalloc]init];

_playerView.controlView= controlView;

}

return_playerView;

}

@end

这样就可以实现视频播放功能了,快操作一下试试吧~

上一篇下一篇

猜你喜欢

热点阅读