欢迎光临外链购买平台,轻松为你获得外链购买的成功案例,友链相关事宜咨询:18978701720,黄经理

外链购买,专注打造匠心好平台

一个好的外链购买,可以为你轻松解决获客难题

AVPlayer实现了通过AVFoundation播放视频和获取视频的缓冲区

作者:jcmp      发布时间:2021-05-14      浏览量:4
本文旨在利用AVPlayer和Buffe

本文旨在利用AVPlayer和Buffer播放本地视频,在获取视频缓冲区的过程中使用AVFoundation获取视频,因为它不是摄像机拍摄的视频,所以我绕道写下来。
项目本身获取视频的需要是获取视频进行进一步处理,不需要播放视频,但播放只是一个句子,我将其注释掉了。
先看效果


1.0加载本地视频,创建Player并播放

<加载本地视频,创建>urlurl=[[nSBundle mainBundle]URLForResource:@“Cat”withExtenence:@“MP4”;AVPlayerItem项目=[[AVPlayerItem]initWithURL];AVPlayer Player=[[AVPlalloayer c]initWithPlayerItem:Item];_Player=Player=[PlayerLayer=[PlayerLayerWithPlayer:Player];Player=[PlayerLayerAVLayerWithPlayer:Player];layer.framework=.1。1创建视频输出

AVPlayerItemVideoOutput*visual Output=[AVPlayerItemVideoOutput alloc]init];[Item addOutput:visual Output];Self.visual Output=visual Output;

1.2一次拿出一个CMSampleBufferRef
显示链接对象,该对象的计时器负责在每个帧需要绘制时向我们发送消息,它将根据监视器的刷新频率同步发送,用于模式:NSRunLoopdes;-(void)displayLinkDidrefresh:(CADisplayLink*)link{CMTimeitemTime=_player.currentItem。当前时间;CVPixelBufferRef像素缓冲=[[]视频输出CopyPixelBufferForItemTime:项目时间项目时间-展示:无];//一次我们到达这个缓冲器,我们能打电话GpuImage或OpenGL为进一步行动//可以将缓冲区转换为UI映像//CIImage*ciImage=[CIImageImageWithCVPixelBuffer:像素缓冲];//CIContext*临时情况=[CIContext]ConextWithOptions:无];//CGImageRef视频图像=[临时情况//]CreateCGImage:CiImage//来自Rect:CGRectMake(0,0,//CVPixelBufferGetWidth(像素缓冲区),//[CVPixelBufferGetHL.8(像素缓冲)];/这个屏风的这个电流框架//UIImage*CurentImage=[UIImage图片CGImage:视频图像];//CGImageReleases(视频图像);//释放在……里面时间不然的话记忆将要高飞CVPixelBufferRelace(像素缓冲);}

全这个电码是如如下:

<预><电码>/创造通过里昂。作者声明:Li on 2017/4/12.//版权c 2017年Leon.li。版权所有。//#导入“XBHomeViewController.h”#import<;AVFoundation/AVFoundation.h;@Interface XBHomeViewController()@Property(非原子,强)AVPlayer*Player*Player;@Property(非原子,强)AVPlayerItemVideoOutput*visual Output;//@Property(非原子,分配)CVPixelBufferRef PielBuffelBuffelBuffelBuffelBuffelBuffelBuffelBuffelBuffelBuffelBuffelBuffelBuffelBuffer;@end@XBHomeViewController-(Void)DidDidLoad{[SuperviewDidLoad];Sel.viewload.backload背景颜色=[UIColor BrownColor];//[Self FiloFile];[Self Player];//加载视图后进行任何附加设置。}-(Void)loadPlayer{NSURL*url=[[NSBundle mainBundle]URLForResource:@“Cat”withExtenence:@“MP4”;AVPlayerItem*Item=[[AVPlayerItem alloc]initWithURL];AVPlayer*Player=[[AVayer Plalloc]initWPlayerItem:Item];_Player=Player=Player;AVerLayer*Layer=[AVerLayer PlayerLayerWithPlayer:Player];layerFrame=自身.Layer addSublayer:Layer];[Player Play];AVPlayerItemVideoOutput*visual Output=[[AVPlayerItemVideoOutput alloc]init];[项目addOutput:visual Output];Self.视频Output=视频Output;//CADisplayLink*link=[CADisplayLink displayLinkWithTarget:Selselector:@selector(DisplayLinkDidTimeNodes)];[链接addToRunloop:[NSRunloop mainRunloop]FormoMode:NSRunLoopCommonModes];//这里为了循环播放[[NotificationCenter defaultCenter]add观察者:Selselector:@selector(Replay):PlayerPlayerToDidTimeNoniNol;]}-(void)displayLinkDidrefresh:(CADisplayLink*)link{CMTimeitemTime=_player.currentItem。CurentTime;CVPixelBufferRef像素缓冲区=[_visual Output CopyPixelBufferForItemTime:itemTime itemTimeForDisplay:nil];//可以将转换为转换为UIImage//ciImage*ciImage=[CIImageWithCVPixelBuffer:PielBuffer];//CIContext*timearyContext=[CIContext contextWOptions:nil];//CGImageRef视频图像=[timearyContext//createCGImage:ciImage//from Rect:CGRectMake(0,0,//CVPixelBufferGetWidth(像素缓冲),//CVPixelBufferGetHight(像素缓冲区))];/当前帧的画面//UIImage*currentImage=[UIImageImageWithCGImage:visual Image];//CGImageReleases(视频图像);//用完记得及时释放掉否则内存会暴涨CVPixelBufferReleases(像素缓冲);}-(Void)回放{type of(Self)弱Self=Self;[Sel.Player SekToTime:kCMTimeZero CompletionHandler:^(BOOL已完成)]{[弱Self]。Player Play];}-(Void)dealloc{[[NSNotificationCenter defaultCenter]Remove观察者:Selve观察者;}-(Void)dicReceiveMemoryWarning{[超级DIDReceiveMemoryWarning];//处置任何可以重新创建的资源。}/*#务实标记-导航//在基于故事板的应用程序中,您通常希望在导航之前做一点准备-(无效)preareareForSegue:(UIStoryboardue*)segue发送方:(Id)发送方使用[sedestinationViewController]获取新的视图控制器。//将选定的对象传递给新的视图控制器.}*/@end