您的位置:首页 > 移动开发 > IOS开发

一个关于AVPlayer蛋疼的释放问题(iOS 9.0 AVPlayer的使用与内存的释放问题)

2015-11-06 16:58 537 查看
首先需要创建AVPlayer  这时候要先包含头文件,因为是9.0了,所以不用包含库,直接导入头文件即可

#import <AVFoundation/AVFoundation.h>


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

@property (nonatomic,strong) AVPlayer *player;//视频播放
@property (nonatomic,strong)AVPlayerLayer *playerLayer;


接着就是创建了

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


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

[self.playerLayer removeFromSuperlayer];
self.playerLayer=nil;
self.player=nil;


搞定!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AVPlayer ios 内存