首先需要创建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];
[self.playerLayer removeFromSuperlayer];
self.playerLayer=nil;
self.player=nil;
搞定!
本文介绍如何在iOS应用中创建并使用AVPlayer进行视频播放。从导入头文件开始,逐步讲解了如何设置播放源、调整播放器大小及位置,并将其添加到视图中。最后还提供了如何正确释放资源的方法。
7763

被折叠的 条评论
为什么被折叠?



