iOS短视频开发之---AVPlayer
基于原生的AVPlayer + ScrollView实现,包含视频播放、暂停、点赞动画实现。视频是采用的本地视频、需要网络视频请自行添加添加测试。
添加方式:
//本地视频 NSString * localPath = [[NSBundle mainBundle] pathForResource:@"视频名称" ofType:@"视频类型"]; NSURL* localUrl = [NSURLURLWithString:localPath]; //在线视频 NSString* webPath =@"链接"; NSURL* webUrl = [NSURLURLWithString:webPath];
核心代码:
AVPlayerItem * avplayerItem = [[AVPlayerItem alloc] initWithURL:url]; self.avplayer = [[AVPlayer alloc] initWithPlayerItem:avplayerItem]; AVPlayerLayer * playLayer = [AVPlayerLayer playerLayerWithPlayer:self.avplayer]; playLayer.videoGravity = AVLayerVideoGravityResize; //默认拉伸方式 playLayer.frame=CGRectMake(0,0,width,height); [self.layer addSublayer:playLayer];
直接上代码:
https://github.com/Turboks/ShortVideo
欢迎star!