//
// ViewController.m
// UI_在线视频
//
// Created by dllo on 16/3/1.
// Copyright © 2016年 dllo. All rights reserved.
//
#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVPlayer *palyer;
@property (strong, nonatomic) IBOutlet UIButton *playButton;
@property (nonatomic, assign) BOOL isPlayer;
@property (nonatomic, strong) UISlider *slider;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 准备播放器
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://mw5.dwstatic.com/2/4/1529/134981-99-1436844583.mp4"]];
self.palyer = [[AVPlayer alloc] initWithPlayerItem:item];
// 播放页面
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.palyer];
playerLayer.frame = CGRectMake(self.view.frame.size.width / 2 - self.view.frame.size.height / 2, self.view.frame.size.height / 2 - self.view.frame.size.width / 2, self.view.frame.size.height, self.view.frame.size.width);
// 旋转至全屏
playerLayer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1);
// 设置显示图层视频的宽高比自动适应播放窗口的大小
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
playerLayer.backgroundColor = [UIColor yellowColor].CGColor;
[self.view.layer addSublayer:playerLayer];
// 设置音量
self.palyer.volume = 1.0f;
// [self.palyer play];
// 将按钮视图层级置顶
[self.view bringSubviewToFront:self.playButton];
self.playButton.transform = CGAffineTransformMakeRotation(M_PI_2);
// 创建slider
self.slider = [[UISlider alloc] init];
self.slider.frame = CGRectMake(0, 0, self.view.frame.size.height - 100, 10);
self.slider.transform = CGAffineTransformMakeRotation(M_PI_2);
self.slider.center = CGPointMake(self.playButton.center.x, self.view.center.y + 50);
[self.view bringSubviewToFront:self.slider];
// 设置最(大)小值
self.slider.maximumValue = 1.0f;
self.slider.minimumValue = 0.0f;
// 添加事件
[self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.slider];
}
// slider事件
- (void)sliderAction:(UISlider *)slider {
// 总帧数 除以 每秒帧数
// self.palyer.currentItem.duration.value / self.palyer.currentItem.duration.timescale
// CMTimeMakeWithSeconds(<#Float64 seconds#>, <#int32_t preferredTimeScale#>)参数
// 参数1 :跳转到的事件
// 参数2 :每秒多少帧
[self.palyer seekToTime:CMTimeMakeWithSeconds(self.palyer.currentItem.duration.value / self.palyer.currentItem.duration.timescale * self.slider.value, self.palyer.currentTime.timescale) completionHandler:^(BOOL finished) {
[self.palyer play];
}];
}
// 播放事件
- (IBAction)playAction:(id)sender {
if (self.isPlayer) {
[self.palyer pause];
[self.playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
self.isPlayer = NO;
} else {
[self.palyer play];
[self.playButton setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
self.isPlayer = YES;
}
}
//- (IBAction)playOrPauseAction:(id)sender {
//
// if (self.isPlayer) {
// [self.palyer pause];
// [self.playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
// self.isPlayer = NO;
// } else {
// [self.palyer play];
// [self.playButton setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
// self.isPlayer = YES;
// }
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end