AVPlayer的简单使用
AVPlayer是cocoa提供的系统播放器组件。下面提供AVPlayer的简单使用方法。复杂的界面定制及横屏支持等操作这里不做介绍。
AVPlayer基本组件介绍:
AVPlayerItem:每条视频资源的管理项,一个item对应一条视频资源
AVPlayerLayer:AVPlayer的播放视图载体,AVPlayer本身不是View类(AVPlayer继承自NSObject类)
seek方法:切换AVPlayer的进度
AVPlayer支持的视频格式(系统播放器支持的视频格式)
H.264、MPEG-4,扩展名(压缩格式):.mp4、.mov、.m4v、.m2v、.3gp、.3g2等
Swift代码
直接贴代码:
var player: AVPlayer?
@IBOutlet weak var slider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// 初始化avplayer
configurePlayer()
}
func configurePlayer() {
let url = URL(string: "http://pri-video.v.medlinker.net/5595b16d-72bc-4fcb-bef2-c01327abeab3/10.m3u8")
// AVPlayerItem用来管理视频资源,一条item对应一个视频资源
let item = AVPlayerItem(url: url!)
// 使用item,初始化avplayer
player = AVPlayer(playerItem: item)
// 使用avplayer初始化AVPlayerLayer,将AVPlayerLayer添加到view上
let layer = AVPlayerLayer(player: player)
view.layer.addSublayer(layer)
layer.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
layer.videoGravity = AVLayerVideoGravityResizeAspect;
}
@IBAction func play(_ sender: Any) {
// rate属性表示avplayer的播放状态,1表示正在播放;0表示暂停播放
if player?.rate == 0 {
player?.play()
}
}
@IBAction func pause(_ sender: Any) {
if player?.rate == 1 {
player?.pause()
}
}
@IBAction func sliderAction(_ sender: Any) {
// 视频的总时间
let sumTime = Float((player?.currentItem?.duration.value)!) / Float((player?.currentItem?.duration.timescale)!)
// 视频要切换的时间
let toTime = sumTime * slider.value
// 切换时间
player?.seek(to: CMTimeMakeWithSeconds(Float64(toTime), Int32((player?.currentItem?.duration.timescale)!)), completionHandler: { (Bool) in
self.player?.play()
})
}
OC代码
@interface ViewController ()
/// 控制视频播放的控件
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
/// 声明播放视频的控件属性[既可以播放视频也可以播放音频]
@property (nonatomic,strong) AVPlayer *player;
/// 播放的总时长
@property (nonatomic,assign) CGFloat sumPlayOperation;
@end
@implementation ViewController
#pragma mark - life cycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configurePlayer];
}
#pragma mark - play methods
- (void)configurePlayer {
// 设置播放的url
NSString *urlStr = @"http://pri-video.v.medlinker.net/5595b16d-72bc-4fcb-bef2-c01327abeab3/10.m3u8";
NSURL *url = [NSURL URLWithString:urlStr];
// 设置播放的项目
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
// 初始化player对象
self.player = [[AVPlayer alloc] initWithPlayerItem:item];
// 设置播放页面
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
// 设置播放页面的大小
layer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300);
layer.backgroundColor = [UIColor cyanColor].CGColor;
// 设置播放窗口和当前视图之间的比例显示内容
layer.videoGravity = AVLayerVideoGravityResizeAspect;
// 添加播放视图到self.view
[self.view.layer addSublayer:layer];
// 设置播放进度的默认值
self.progressSlider.value = 0;
// 设置播放的默认音量值
self.player.volume = 1.0f;
}
#pragma mark - event response
- (IBAction)startPlayer:(id)sender {
[self.player play];
}
- (IBAction)stopPlayer:(id)sender {
[self.player pause];
}
// change pregress
- (IBAction)changeProgress:(id)sender {
self.sumPlayOperation = self.player.currentItem.duration.value / self.player.currentItem.duration.timescale;
// CMTimeMake(a,b) a表示当前时间,b表示每秒钟有多少帧
[self.player seekToTime:CMTimeMakeWithSeconds(self.progressSlider.value * self.sumPlayOperation, self.player.currentItem.duration.timescale) completionHandler:^(BOOL finished) {
[self.player play];
}];
}
@end