iOS求生之路五(iOS 9.0 AVPlayer的使用与内存的释放问题)

iOS求生之路五(iOS 9.0 AVPlayer的使用与内存的释放问题)

标签: AVPlayerios内存
  1190人阅读  评论(0)  收藏  举报
  分类:

首先需要创建AVPlayer  这时候要先包含头文件,因为是9.0了,所以不用包含库,直接导入头文件即可

[objc]  view plain  copy
  1. #import <AVFoundation/AVFoundation.h>  

在这里可以把播放器作为成员变量,方便全局使用,当然,也可以不用,我在这里是作为全局变量来使用的,方便内存的管理

[objc]  view plain  copy
  1. @property (nonatomic,strongAVPlayer *player;//视频播放  
  2. @property (nonatomic,strong)AVPlayerLayer *playerLayer;  

接着就是创建了

[objc]  view plain  copy
  1. //创建视频播放器  
  2. NSString *filePath =[[NSBundle mainBundle]pathForResource:@"flash" ofType:@"mp4"];  
  3. NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];  
  4. //初学者这里先不要管,但是必须要创建  
  5. AVAsset *movieAsset    = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];  
  6.   
  7. AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];  
  8. _player = [AVPlayer playerWithPlayerItem:playerItem];  
  9. _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];  
[objc]  view plain  copy
  1.  //大小  
  2. _playerLayer.frame = CGRectMake(ScreenWidth/4.4, ScreenHeight/3.3, _coverView.pictureAndAvView.frame.size.width, _coverView.pictureAndAvView.frame.size.height+100);  
  3. _playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;  
  4. _playerLayer.backgroundColor  = [UIColor blackColor].CGColor;  
[objc]  view plain  copy
  1.  //要添加的地方  
  2. [_coverView.AirBubble.layer addSublayer:_playerLayer];  
  3. [_player play];  



用完是需要注意要对其进行释放:写在你退出的点击事件当中,比如说要pop视图了

[objc]  view plain  copy
  1. [self.playerLayer removeFromSuperlayer];  
  2.       
  3.       
  4.     self.playerLayer=nil;  
  5.       
  6.       
  7.     self.player=nil;  


搞定!
/
 前两天发现了导致这个问题的根源是没有释放播放器的playeritem  所以还在缓冲  释放播放器时加上两句
[playerItem cancelPendingSeeks];
[playerItem.asset cancelLoading];
停掉playeritem的网络请求释放掉playerintm 就不会再出现这种问题了 

[player.currentItem cancelPendingSeeks];
[player.currentItem.asset cancelLoading];
加这两行代码,将正在播放的 item 释放掉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值