自定义播放器
来源:互联网 发布:家庭网络 交换机 编辑:程序博客网 时间:2024/06/10 16:54
//播放的视图@property (weak, nonatomic) IBOutlet UIView *playerView;//用来控制的底部视图@property (weak, nonatomic) IBOutlet UIView *bottomView;//开始按钮- (IBAction)startButton:(id)sender;//按钮属性@property (weak, nonatomic) IBOutlet UIButton *startButton;//用来显示时间的label@property (weak, nonatomic) IBOutlet UILabel *timeLabel;//用来控制快进等功能的slider的触发方法- (IBAction)sliderAction:(id)sender;//用来修改进度内容的进度条@property (weak, nonatomic) IBOutlet UISlider *progressSlider;@property (nonatomic,retain) AVPlayer *player;@property (nonatomic,retain) AVPlayerItem *playerItem;@property (nonatomic,retain) AVPlayerLayer *playerLayer;@property (nonatomic,assign) BOOL isStart;@property (nonatomic,assign) BOOL isTap;//用来记录视频时长,快进快退的时候需要使用@property (nonatomic,assign) CGFloat duation;@end@implementation PlayerViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //初始化播放器 [self createPlayer]; //播放 [_player play]; //给当前屏幕,添加一个监听横竖屏和视频结束的方法 [self addNotificationCenters]; //创建一个轻点的手势 [self createTap]; //对播放进度进行设置 [self addProgress]; _timeLabel.text = @"00:00/00:00"; _isStart = YES;}//CMTime是专门用来表示视频时间的//CMTimeMake是用来创建CMTime的//用法就是CMTTimeMake(time,timeScale)//time指的是时间,但不是秒,如果想转换成秒需要通过第二个参数来完成//timeScale值得是一秒需要多少帧构成,真正的时间是time/timeScale#pragma mark 添加进度的设置-(void)addProgress{ //设置成(1,1)每秒执行一次 [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) { //视频的总时间 将CMTime转换成CGFloat CGFloat durationTime = CMTimeGetSeconds(_playerItem.duration); //当前时间 CGFloat current = CMTimeGetSeconds(_player.currentTime); //倒计时 CGFloat rem = durationTime - current; //把时间转换成NSString,再进行赋值 NSString *tltaleT = [NSString stringWithFormat:@"%02d:%02d",(int)rem / 60,(int)rem %60]; NSString *currentT = [NSString stringWithFormat:@"%02d:%02d",(int)current / 60,(int)current %60]; NSString *timeStr = [NSString stringWithFormat:@"%@/%@",currentT,tltaleT]; //最后给label进行赋值 _timeLabel.text = timeStr; _progressSlider.value = current / durationTime; //保存总时长,用于手动快进 _duation = durationTime; }];}#pragma mark 轻点手势-(void)createTap{ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; [self.view addGestureRecognizer:tap];}#pragma mark 轻点之后,实现视图的显示和隐藏-(void)tapAction:(UITapGestureRecognizer *)tap{ if (_isTap == NO) { [UIView animateWithDuration:0.5 animations:^{ _bottomView.alpha = 0; }]; }else{ [UIView animateWithDuration:0.5 animations:^{ _bottomView.alpha = 1; }]; } _isTap = !_isTap;}#pragma mark 添加监听方法-(void)addNotificationCenters{ //监听当前的视频播放情况,只要播放结束,就出发指定方法 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil]; //监听当前屏幕的旋转 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenChange:) name: UIApplicationDidChangeStatusBarOrientationNotification object:nil];}#pragma mark 监听屏幕旋转-(void)screenChange:(NSNotification *)notification{ UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; //根据横竖屏,分别设置播放器的frame if (orientation == UIInterfaceOrientationLandscapeLeft) { [self setPlayerLayerFarme]; }else if (orientation == UIInterfaceOrientationLandscapeRight) { [self setPlayerLayerFarme]; }else if (orientation == UIInterfaceOrientationPortrait){ [self setPortraitFarme]; } }#pragma mark 设置横屏时候的尺寸-(void)setPlayerLayerFarme{ _playerLayer.frame = CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - 44); _timeLabel.font = [UIFont systemFontOfSize:15];}#pragma mark 设置竖屏时候的尺寸-(void)setPortraitFarme{ _playerLayer.frame = CGRectMake(5, 0, SCREEN_WIDTH - 10, 300); _timeLabel.font = [UIFont systemFontOfSize:11];}#pragma mark 监听播放结束-(void)movieEnd:(NSNotification *)notification{ NSLog(@"播放结束了");}#pragma mark 初始化播放器-(void)createPlayer{ NSString *urlStr = @"http://flv2.bn.netease.com/videolib3/1505/24/HYUCE6348/SD/HYUCE6348-mobile.mp4"; //对网址进行编码 urlStr =[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSURL *url = [NSURL URLWithString:urlStr]; //根据网址,创建视频项目对象 _playerItem = [[AVPlayerItem alloc] initWithURL:url]; //创建视频播放器对象 _player = [AVPlayer playerWithPlayerItem:_playerItem]; //创建一个播放器承载对象,把播放器放到上面 _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player]; //设置一下承载对象的尺寸 _playerLayer.frame = CGRectMake(5,5, SCREEN_WIDTH, 290); //视频的填充方式 _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; //把播放器添加到layer层上 [_playerView.layer insertSublayer:_playerLayer atIndex:0]; //给一个背景颜色 _playerView.backgroundColor = [UIColor whiteColor];}#pragma mark 手动快进- (IBAction)sliderAction:(id)sender { //获取当前的时间 double currentTime = _duation * _progressSlider.value; //设置要进行跳转的时间 CMTime drage = CMTimeMake(currentTime, 1); //对player进行设置 [_player seekToTime:drage completionHandler:^(BOOL finished) { [_player play]; }]; }#pragma mark 播放暂停- (IBAction)startButton:(id)sender { if (_isStart == YES) { [_startButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal]; [_player pause]; }else{ [_startButton setImage:[UIImage imageNamed:@"start.png"] forState:UIControlStateNormal]; [_player play]; } _isStart = !_isStart;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
0 0
- html5 自定义播放器
- 自定义视频播放器
- android自定义播放器
- 自定义音频播放器
- 自定义播放器
- 自定义的播放器
- iOS 自定义播放器
- 自定义HTML5播放器
- 自定义视频播放器
- 自定义视频播放器
- 自定义播放器
- 自定义视频播放器
- Android-自定义播放器
- android 自定义播放器播放视频
- 自定义HTML5 audio 播放器
- android自定义视频播放器
- Android自定义音乐播放器
- html5 audio 自定义播放器
- android ndk编译,方法找不到,collect2: ld returned 1 exit status
- Elasticsearch Java API 开发
- win10 ssd 卡顿
- setTimeout无效的解决方案(三)
- 不兼容结构的协调——适配器模式(一):适配器模式概述
- 自定义播放器
- 16-01-14 EditText 属性
- (整理转载)消息队列(MQ)简介与应用场景
- Python -- 三元表达式(三目运算符)
- C# 获取访问者的IP地址
- js过滤数组中相同的值
- 关于oracle死锁问题——ORA-01436: 用户数据中的 CONNECT BY 循环
- 布局
- 策略模式