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

来源:互联网 发布:扫雷java 需求分析 编辑:程序博客网 时间:2024/06/11 05:06

首先需要创建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;


搞定!

0 0
原创粉丝点击